There are generally three types of JavaScript date input formats: ISO Date, Short Date, and Long Date. The ISO format is the preferred and most reliable standard in JavaScript.
The ISO 8601 syntax (YYYY-MM-DD) is the preferred JavaScript date format. It is also the format most commonly used by databases.
const d = new Date("2022-03-25"); // YYYY-MM-DD
Short dates are written with an "MM/DD/YYYY" syntax like this:
"03/25/2022".
Long dates are most often written with a "MMM DD YYYY" syntax like this:
"Mar 25 2022".
If you have a valid date string, you can use the Date.parse()
method to convert it into milliseconds. This is very useful for calculations.
let msec = Date.parse("March 21, 2012");
const d = new Date(msec); // Create date from milliseconds
When setting a date, without specifying a time zone, JavaScript will use the browser's time zone. When getting a date, without specifying a time zone, the result is converted to the browser's time zone.
Date.parse() converts date strings to milliseconds