Date Object
Date is another built-in object in JavaScript. The sections below go over what you can do with it.
Constructing Dates
-
Date is measured in
millisecondssince theepoch(midnight of January 1, 1970 UTC).let now = new Date(); // yields current date/time. console.log(now); -
You can pass a number of
millisecondsto the constructor. The valid range is ±100,000,000 days from the epoch.// new Date(milliseconds) constructs Date from milliseconds. console.log(new Date(10 ** 12));
-
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
dayis optional.const brendanEichsBirthday = new Date(1961, 6 /* July */, 4); console.log(brendanEichsBirthday); // Tue Jul 04 1961 00:00:00 GMT-0400 (Eastern Daylight Time)
const invalidDate = new Date(2019, 13, -2);
console.log(invalidDate); // Wed Jan 29 2020 00:00:00 GMT-0500 (Eastern Standard Time)
-
To construct a date in UTC, pass a date string in the format
YYYY-MM-DDTHH:mm:ss.sssZ, with a literal T and a literal Z:const firstMillennialUTCnoon = new Date("2000-01-01T12:00:00.000Z"); console.log(firstMillennialUTCnoon); // Sat Jan 01 2000 07:00:00 GMT-0500 (Eastern Standard Time)
const now = Date();
console.log(now);
Static Date Functions
-
Date.now()yields the current time in milliseconds, not a Date object.const now = Date.now(); console.log(now); -
Another 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 aliBirthday = Date.parse("May 9, 1985"); console.log(aliBirthday); // 484459200000 aliBirthday = new Date(aliBirthday); console.log(aliBirthday); // Thu May 09 1985 00:00:00 GMT-0400 (Eastern Daylight Time)
- Another static function is
Date.UTC(year, zeroBasedMonth, day, hours, minutes, seconds, milliseconds)where arguments afteryearare optional.
Getters and Setters
-
The Date object has the usual getter and setter methods, such as
getHoursandsetHours.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 aliBirthday = new Date(); aliBirthday.setFullYear(1985); aliBirthday.setMonth(4); // May aliBirthday.setDate(9); console.log(aliBirthday); -
For UTC, there are UTC variants
getUTCFullYear,setUTCFullYear,getUTCMonth,setUTCMonth, and so on.
Date Formatting
-
toISOStringyields a string in theYYYY-MM-DDTHH:mm:ss.sssZformat.let now = new Date(); console.log(now.toISOString()); -
toString,toDateString, andtoTimeStringyield a human-readable string in the local time zone, or only the date or time portion of it:let now = new Date(); console.log(now.toString()); console.log(now.toDateString()); console.log(now.toTimeString()); -
toUTCStringyields a human-readable string in UTC:let now = new Date(); console.log(now.toUTCString()); -
toLocaleString,toLocaleDateString, andtoLocaleTimeStringyield a localized string:let now = new Date(); console.log(now.toLocaleTimeString("en-US")); console.log(now.toLocaleTimeString("zh-Hans-CN-u-nu-hanidec"));