HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Date Formats

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.


1. ISO Dates (International Standard)

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
  • YYYY-MM: Year and month (e.g., "2022-03")
  • YYYY: Year only (e.g., "2022")

2. Short and Long Dates

Short Dates

Short dates are written with an "MM/DD/YYYY" syntax like this: "03/25/2022".

Long Dates

Long dates are most often written with a "MMM DD YYYY" syntax like this: "Mar 25 2022".


Parsing Dates

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
Warning: In some browsers, months or days with no leading zeroes may produce an error. Always try to use leading zeroes for better compatibility (e.g., "01" instead of "1").

Time Zones

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.


Key Points to Remember

  • ISO 8601 (YYYY-MM-DD) is the industry standard
  • Date.parse() converts date strings to milliseconds
  • Always use leading zeroes for month and day to ensure browser compatibility
  • Date strings can be unpredictable; stick to ISO format whenever possible
  • Without specific zone info, the browser's local time is used
  • Year, Month, and Day are the primary components of all date formats