Converting


const d = new Date("2021-03-25"); 
const d = new Date("2021-03");
const d = new Date("2021");

Date and time is separated with a capital T.
UTC time is defined with a capital letter Z.

const d = new Date("2021-03-25T12:00:00Z"); 

Short Dates

Short dates are written as "MM/DD/YYYY"
Always include leading zeros.

const d = new Date("03/25/2021"); 


Long Dates

Long dates are typically written as "MMM DD YYYY"
The month and day can be in any order.
Commas are ignored and names are NOT case sensitive:

const d = new Date("Mar 25 2021"); 
const d = new Date("25 Mar 2021");
const d = new Date("January 25 2015");
const d = new Date("Jan 25 2015");
const d = new Date("JANUARY, 25, 2015");


Date.parse()

Date.parse() returns the number of milliseconds between the date and January 1, 1970:
You can then use the number of milliseconds to convert it to a date object.

let msec = Date.parse("March 21, 2012"); 
const d = new Date(msec);



© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext