The ISO 8601 standard defines the parts of dates as follows:
- MM — month from 01 to 12
- MMM — month abbreviation from Jan. to Dec.
- DD — day from 01 to last day of the month (various)
- YYYY — year as a 4-digit number
The ISO 8601 standard defines the parts of times as follows:
- T — separation character between date and time
- HH — hours from 00 to 23
- MM — minutes from 00 to 59
- SS — seconds from 00 to 59
- Z — indicates Coordinated Universal Time (UTC)
- +HH:MM — replaces “Z” if offsetting UTC to another time zone later than UTC
- -HH:MM — replaces “Z” if offsetting UTC to another time zone earlier than UTC
- Dates Only — YYYY-MM-DD
- Dates With Times — YYYY-MM-DDTHH:MM:SSZ
let completeDate = new Date("2021-01-01"); let yearMonth = new Date("2021-01"); let yearOnly = new Date("2021"); let dateTimeUTC = new Date("2021-01-01T12:00:00Z"); let dateTimeEST = new Date("2021-01-01T12:00:00-04:00");
dateTimeEST refer to time zones; respectively Coordinated Universal Time and Eastern Standard Time. Location-appropriate presentation to the user is as important as ensuring time issues remain consistent for all users, regardless of location.
let shortDate = new Date("01/12/2021");
You can write months’ full names or abbreviate them in long dates. Long dates ignore commas and are case-insensitive.
// These long dates are equivalent. let longDate1 = new Date("Jan 01 2021"); let longDates = new Date("01 Jan 2021"); let longDate3 = new Date("January 01 2021"); let longDate4 = new Date("JAN 01, 2021");
Months or Days Without Leading Zeros
In some browsers, specifying a date without leading zeros on months or days may cause an error in the “YYYY-MM-DD” format. The following examples should be equivalent, but only the first one is guaranteed not to create an error.
// These three dates should be equivalent, but are NOT. let leading1 = new Date("2021-01-01"); // OK let leading2 = new Date("2021-1-01"); // Possible month error let leading3 = new Date("2021-01-1"); // Possible day error
Undefined Date Formats
Always specify dates with their components in the recommended order. Switching months, days, and years around may cause unexpected errors. The following examples illustrate common problems with undefined date formats, assuming a date of November 24, 2021.
The date format “YYYY/MM/DD” (with forward-slashes as delimiter) is undefined. Some browsers will try to read the format, and may throw an error if they cannot, and others will return NaN (not a number). Always specify short dates in the “MM/DD/YYYY” format.
let short1 = new Date("11/24/2021"); // Correct let short2 = new Date("2021/11/24"); // Possible read, error, or NaN
When specifying ISO dates, the format MM-DD-YYYY is undefined. Like the previous format, some browsers will try to read it, and they might throw an error or return NaN. Always specify ISO dates in the “YYYY-MM-DD” format.
let iso1 = new Date("2021-11-24"); // Correct let iso2 = new Date("11-24-2021"); // Possible read, error, or NaN
Time Zone Variations
// This date may compute to December 31, 2020 or January 1, 2021. let tz1 = new Date("2021-01-01");
Always specify full dates and times with the proper format to avoid this problem. Don’t forget to add the “T” date/time separator and to use “Z” or “+/-HH:MM” when specifying full dates and times, or your program may still be vulnerable to these variations.
Specifying Dates for International Audiences
Always use ISO dates when specifying dates for international audiences. The standard formats of short dates could cause confusion for international audiences, as could the presence of undefined date formats.
// ISO standard date for April 8, 2021 let iso = new Date("2021-04-08"); // Short dates, written as a US or international speaker would let shortUSA = new Date("04-08-2021"); // Error let shortInt = new Date("2021/04/08"); // Error