Date
The Date
is another built-in object in JavaScript. We used it in the development of the SleepTime App. Here is a summary of its operations. For a more detailed reference, visit MDN web docs.
Constructing Dates
- Date measured in "milliseconds" (adjusted for leap seconds) since the "epoch" (midnight of January 1, 1970 UTC).
let now = new Date(); // yields current date/time. console.log(now);
- You can pass milliseconds to the constructor; valid range ±100,000,000 days from epoch.
// new Date(milliseconds) constructs Date from milliseconds. console.log(new Date(10**12));
Caution: Dates are converted to numbers in arithmetic expressions.
-
You can pass the following parameters to
Date()
constructor to create a specific date:year
,zeroBasedMonth
,day
,hours
,minutes
,seconds
,milliseconds
-
The parameters must be provided in order, and everything starting from "day" are optional.
const brendanEichsBirthday = new Date(1961, 6 /* July */, 4); console.log(brendanEichsBirthday);
Caution: Out-of-range zeroBasedMonth, day, hours, etc. silently roll.
const invalidDate = new Date(2019, 13, -2); // 2020-01-28 console.log(invalidDate);
- For UTC, construct with date string in format YYYY-MM-DDTHH:mm:ss.sssZ (with literal T, Z):
const firstMillennialUTCnoon = new Date('2000-01-01T12:00:00.000Z'); console.log(firstMillennialUTCnoon);
Caution: Other string formats may be accepted by the constructor, but their format is not standardized.
Caution:
Date(...)
withoutnew
constructs string (in non-standard format).
const now = Date(); console.log(now);
Static Date Functions
Date.now()
is the current date/time, but it yields milliseconds, not a Date object.
const now = Date.now(); console.log(now);
- A very useful static function is
Date.parse(dateString)
. It also yields milliseconds, but you can pass that to the Date constructor to get a Date object.
let alexBirthday = Date.parse("May 1, 1980"); console.log(alexBirthday); alexBirthday = new Date(alexBirthday); console.log(alexBirthday);
Caution: support for
dateString
inYYYY-MM-DDTHH:mm:ss.sssZ
format is guaranteed. Support for other formats are implementation-dependent.
- Another useful static function is
Date.UTC(year, zeroBasedMonth, day, hours, minutes, seconds, milliseconds)
where arguments afteryear
are optional.
Getters and Setters
- The Date object has traditional getter/setter methods
getHours
/setHours
.
const now = new Date(); console.log(now.getFullYear()); console.log(now.getMonth()); // 0-11 console.log(now.getDate()); // 1-31 console.log(now.getHours()); // 0-23 console.log(now.getMinutes()); console.log(now.getSeconds()); console.log(now.getMilliseconds());
let alexBirthday = new Date(); alexBirthday.setYear(1985); alexBirthday.setMonth(5); // May alexBirthday.setDate(1); console.log(alexBirthday);
- For UTC, there are UTC variants
getUTCFullYear
,setUTCFullYear
,getUTCMonth
,setUTCMonth
, and so on.
Date Formatting
toISOString
yields string inYYYY-MM-DDTHH:mm:ss.sssZ
format.
let now = new Date(); console.log(now.toISOString());
toString
,toDateString
,toTimeString
yield "humanly readable" string in local time zone, or only the date/time portion:
let now = new Date(); console.log(now.toString()); console.log(now.toDateString()); console.log(now.toTimeString());
toUTCString
yields "humanly readable" string in UTC:
let now = new Date(); console.log(now.toUTCString());
toLocaleString
,toLocaleDateString
,toLocaleTimeString
yield localized string:
let now = new Date(); console.log(now.toLocaleTimeString("en-US")); console.log(now.toLocaleTimeString("zh-Hans-CN-u-nu-hanidec"));