JavaScript Introduction | Standard Objects | Date Object
Date Representation in JavaScript
In JavaScript, the Date object lets you easily obtain information about constantly changing time and dates.
The Date object provides year, month, day, hour, minute, and second information, as well as millisecond information.
The ranges of values for representing dates and times in JavaScript are as follows.
- Year: 1900 (00) to 2018 (99)
- Month: January (0) to December (11)
- Day: 1 to 31
- Hours: 0 to 23
- Minutes: 0 to 59
- Seconds: 0 to 59
When representing months in JavaScript, note that January is represented as 0 and December is represented as 11.
Date Object
The following are ways to initialize a Date object in JavaScript.
- new Date()
- new Date(“string representing a date”)
- new Date(milliseconds)
- new Date(year, month, day, hour, minute, second, millisecond)
If no argument is passed when creating a Date object, a Date object is created with the current date and time.
var date = new Date(); // creates a Date object
document.write(date);
If arguments are passed when creating a Date object, a Date object pointing to a specific date and time is created according to the form of those arguments.
new Date("December 14, 1977 13:30:00"); // string representing a date
new Date(80000000); // date after that many milliseconds since January 1, 1970, 00:00
new Date(16, 5, 25); // date represented by three numbers; time is automatically set to 00:00:00.
new Date(16, 5, 25, 15, 40, 0); // date represented by seven numbers
new Date(2016, 5, 25, 15, 40, 0); // to represent the 2000s, write the full year.
All JavaScript date calculations are based on January 1, 1970 00:00:00 (UTC, Coordinated Universal Time). Also, one day is calculated as 86,400,000 milliseconds.
JavaScript Date Formats
The following formats can represent dates in JavaScript.
- ISO date format
- Long date format
- Short date format
- Full date format
ISO 8601 is an international standard format for representing dates and times.
Syntax
YYYY-MM-DDTHH:MM:SS // T is a character representing UTC and must be used when expressing time as well.
YYYY-MM-DD
YYYY-MM
new Date("1977-12-14T13:30:00"); // represents both date and time.
new Date("1977-12-14"); // if time is omitted, it is automatically set to 09:00:00.
new Date("1977-12"); // if day is omitted, it is automatically set to the 1st.
new Date("1977"); // if month is omitted, it is automatically set to January.
Long Date Format
Syntax
MMM DD YYYY
DD MMM YYYY
new Date("Feb 19 1982"); // MMM DD YYYY
new Date("19 Feb 1982"); // DD MMM YYYY
new Date("February 19 1982"); // recognizes full month words as well as abbreviations.
new Date("FEBRUARY, 19, 1982"); // commas are ignored, and case is not distinguished.
Short Date Format
Syntax
MM/DD/YYYY
YYYY/MM/DD
new Date("02/19/1982"); // MM/DD/YYYY
new Date("1982/02/19"); // YYYY/MM/DD
In ISO date format and short date format, dates must appear in month-day order. JavaScript may not properly recognize dates written in day-month order.
Full Date Format
Strings expressed in the date format used by JavaScript are also recognized as dates.
Wed May 25 2016 17:00:31 GMT+0900
new Date("Wed May 25 2016 17:00:00 GMT+0900 (Seoul Time)");
// If GMT differs from the current country, it is converted and expressed in the current country's GMT.
new Date("Wed May 25 2016 17:00:00 GMT-0500 (New York Time)");