JavaScript Date Format
- Previous Page JS Date
- Next Page JS Date Retrieval Method
There are four JavaScript date input formats:
Type | Example |
---|---|
ISO date | "2018-02-19" (international standard) |
Short date | "02/19/2018" or "2018/02/19" |
Long date | "Feb 19 2018" or "19 Feb 2019" |
Full date | "Monday February 25 2015" |
ISO format complies with the strict standards in JavaScript.
Other formats are not very clear and may be browser-specific.
JavaScript date output
Regardless of the input format, JavaScript defaults to outputting the full text string format:
Mon Feb 19 2018 06:00:00 GMT+0800 (China Standard Time)
JavaScript ISO date
ISO 8601 is the international standard for representing dates and times.
ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format:
Example (full date)
var d = new Date("2018-02-19");
The calculated date is relative to your time zone.
According to your time zone, the above result will change between February 18 and February 19.
ISO date (year and month)
The date can also be written without specifying a specific day (YYYY-MM):
var d = new Date("2015-03");
The timezone may cause changes to the result between February 28 and March 1.
ISO date (only year)
Writing the date can also not specify the specific month and day (YYYY):
var d = new Date("2018");
The timezone may cause changes to the result between December 31, 2017, and January 1, 2018.
ISO date (complete date with time, minutes, and seconds)
Writing the date can also add hours, minutes, and seconds (YYYY-MM-DDTHH:MM:SS):
var d = new Date("2018-02-19T12:00:00");
The date and time are separated by the uppercase letter 'T'.
UTC time is defined by the uppercase letter 'Z'.
If you want to modify the time relative to UTC, remove 'Z' and replace it with +HH:MM or -HH:MM:
Example
var d = new Date("2018-02-19T12:00:00-08:30");
UTC (Universal Time Coordinated) is equivalent to GMT (Greenwich Mean Time).
Note:UTC, Coordinated Universal Time, also known as World Unified Time, World Standard Time, International Atomic Time.
Omitting 'T' or 'Z' in the date-time string may produce different results in different browsers.
Timezone
When setting the date, if the timezone is not specified, JavaScript will use the browser's timezone.
When getting the date, if the timezone is not specified, the result will be converted to the browser's timezone.
In other words, if the date/time is created in GMT (Greenwich Mean Time), the date/time will be converted to CST (China Standard Time) if the user is browsing from China.
JavaScript short date
Short dates are usually written in the syntax such as "MM/DD/YYYY":
Example
var d = new Date("02/19/2018");
Warning
In some browsers, the month or day without a leading zero may cause an error:
var d = new Date("2018-2-19");
the behavior of "YYYY / MM / DD" is undefined.
Some browsers may try to guess the format. Some will return NaN
。
var d = new Date("2018/02/19");
the behavior of "DD-MM-YYYY" is also undefined.
Some browsers may try to guess the format. Some will return NaN
。
var d = new Date("19-02-2018");
JavaScript long date
Long dates are usually written in the syntax such as "MMM DD YYYY":
Example
var d = new Date("Feb 19 2018");
Months and days can appear in any order:
Example
var d = new Date("19 Feb 2018");
And, months can be written in full name (January) or abbreviation (Jan):
Example
var d = new Date("February 19 2018");
Example
var d = new Date("Feb 19 2018");
Commas will be ignored and are case-insensitive:
Example
var d = new Date("FEBRUARY, 25, 2015");
JavaScript Complete Date
JavaScript accepts date strings in the "full JavaScript format":
Example
var d = new Date("Mon Feb 19 2018 06:55:23 GMT+0100 (W. Europe Standard Time)");
JavaScript will ignore errors in the date name and time brackets:
Example
var d = new Date("Fri Mar 26 2018 09:56:24 GMT+0100 (Tokyo Time)");
- Previous Page JS Date
- Next Page JS Date Retrieval Method