Convert string to date javascript yyyy/mm/dd

Split on "-"

Parse the string into the parts you need:

var from = $("#datepicker").val().split("-")
var f = new Date(from[2], from[1] - 1, from[0])

Use regex

var date = new Date("15-05-2018".replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"))

Why not use regex?

Because you know you'll be working on a string made up of three parts, separated by hyphens.

However, if you were looking for that same string within another string, regex would be the way to go.

Reuse

Because you're doing this more than once in your sample code, and maybe elsewhere in your code base, wrap it up in a function:

function toDate(dateStr) {
  var parts = dateStr.split("-")
  return new Date(parts[2], parts[1] - 1, parts[0])
}

Using as:

var from = $("#datepicker").val()
var to = $("#datepickertwo").val()
var f = toDate(from)
var t = toDate(to)

Or if you don't mind jQuery in your function:

function toDate(selector) {
  var from = $(selector).val().split("-")
  return new Date(from[2], from[1] - 1, from[0])
}

Using as:

var f = toDate("#datepicker")
var t = toDate("#datepickertwo")

Modern JavaScript

If you're able to use more modern JS, array destructuring is a nice touch also:

const toDate = (dateStr) => {
  const [day, month, year] = dateStr.split("-")
  return new Date(year, month - 1, day)
}

Convert a dd/mm/yyyy string to a Date in JavaScript #

To convert a dd/mm/yyyy string to a date:

  1. Split the string on each forward slash to get the day, month and year.
  2. Pass the year, month minus 1 and the day to the Date() constructor.
  3. The Date() constructor creates and returns a new Date object.

Copied!

const str = '22/04/2022'; const [day, month, year] = str.split('/'); console.log(day); // 👉️ 22 console.log(month); // 👉️ 04 console.log(year); // 👉️ 2022 const date = new Date(+year, month - 1, +day); console.log(date); // 👉️ Fri Apr 22 2022

We used the split method to split the string on each forward slash /.

The method returns an array containing the substrings.

Copied!

const str = '22/04/2022'; // 👇️ ['22', '04', '2022'] console.log(str.split('/'));

We used array destructuring to assign the values to variables in a single statement.

The Date() constructor takes the year, month (0 = January to 11 = December), day of the month, hours, minutes and seconds as parameters and returns a Date object.

We used the unary plus (+) operator to convert the strings to numbers when passing them to the Date() constructor.

Notice that we subtracted 1 from the month when passing it to the Date() constructor.

This is because, the Date constructor expects a zero-based value, where January = 0, February = 1, March = 2, etc.

Here is an example that converts a date and time string formatted as dd/mm/yyyy hh:mm:ss to a Date object.

Copied!

const str = '22/04/2022 07:30:16'; const [dateComponents, timeComponents] = str.split(' '); console.log(dateComponents); // 👉️ "22/04/2022" console.log(timeComponents); // 👉️ "07:30:16" const [day, month, year] = dateComponents.split('/'); const [hours, minutes, seconds] = timeComponents.split(':'); const date = new Date(+year, month - 1, +day, +hours, +minutes, +seconds); console.log(date); // 👉️ Fri Apr 22 2022 07:30:16

The first thing we did was split the date and time string on the space, so we can get the date and time components as separate strings.

We then had to split the date string on each forward slash to get the value for the day, month and year. The approach is the same even if the separator is different, e.g. a hyphen.

We also split the time string on each colon and assigned the hours, minutes and seconds to variables.

Notice that we subtracted 1 from the month when passing it to the Date() constructor.

This is because, the Date constructor expects a zero-based value for the month, where January = 0, February = 1, March = 2, etc.

We then passed all of the parameters to the Date() constructor to create a Date object.

How convert dd mm yyyy string to date in JavaScript?

To convert a dd/mm/yyyy string to a date: Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.

Can we convert string to date in JavaScript?

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

How do I get date in YYYY MM DD format typescript?

“get current date in typescript in mm/dd/yyyy format” Code Answer's.
var today = new Date();.
var dd = String(today. getDate()). padStart(2, '0');.
var mm = String(today. getMonth() + 1). padStart(2, '0'); //January is 0!.
var yyyy = today. getFullYear();.
today = mm + '/' + dd + '/' + yyyy;.
document. write(today);.