Date and Time
The Date
class abstracts most of the operations with dates. This makes it possible to think of moments in time as objects and manipulate them with predefined methods. Using various capabilities of the Date
class, you can create clocks, counters, calendars and other interactive interface elements.
Creating date
A Date
object instance is an object representing a specific point in time. Creating a date without arguments returns an object that stores the date and time at its initialization time, that is, the current one. In string conversion, the object returns the result of calling the toString()
method, so in the first log you get a string, not an object.
const date = new Date();
console.log(date);
// "Fri Jun 18 2021 15:01:35 GMT+0300 (Eastern European Summer Time)"
console.log(date.toString());
// "Fri Jun 18 2021 15:01:35 GMT+0300 (Eastern European Summer Time)"
Unix time
Dates are represented in milliseconds since midnight on January 1, 1970 in the UTC time zone. For a computer, this is the starting date of all time, Unix time. Therefore, when initializing a date with a single number, it represents the number of milliseconds that have passed.
console.log(new Date(0));
// "Thu Jan 01 1970 03:00:00 GMT+0300 (Eastern European Standard Time)"
console.log(new Date(15000));
// "Thu Jan 01 1970 3:00:15 AM GMT+0300 (Eastern European Standard Time)"
The getTime()
method returns a numeric representation of the date (timestamp
) – the number of milliseconds that have passed since midnight on January 1, 1970.
const date = new Date();
console.log(date.getTime()); // 1624021654154
The convenience of this format lies in the ability to represent exact moments in time as a single number, without having to worry about dates, strings and time zones, since you can get all the necessary information when you need it.
Setting date
When instantiating the Date
class, you can set the date to a string or numbers. The string can describe a date only or a date and time.
const teamMeetingDate = new Date("March 16, 2030");
console.log(teamMeeting);
// "Mon Mar 16 2030 00:00:00 GMT+0200 (Eastern European Standard Time)"
const preciseTeamMeetingDate = new Date("March 16, 2030 14:25:00");
console.log(preciseTeamMeetingDate);
// "Mon Mar 16 2030 2:25:00 PM GMT+0200 (Eastern European Standard Time)"
Setting the time as strings internally calls the Date.parse()
method, which converts the string to a number, i.e. the number of milliseconds. This is why the passed string format is very flexible. For example, you can omit zero for days and months. Let's look at several examples that will produce the same result.
new Date("2030-03-16");
new Date("2030-03");
new Date("2018");
new Date("03/16/2030");
new Date("2030/03/16");
new Date("2030/3/16");
new Date("March 16, 2030");
new Date("March 16, 2030 14:25:00");
new Date("2030-03-16 14:25:00");
new Date("2030-03-16T14:25:00");
new Date("16 March 2030");
Another way to create new objects is to pass seven numbers that describe the year, month (starting at 0), day, hours, minutes, seconds and milliseconds. Only the first three are mandatory.
const date = new Date(2030, 2, 16, 14, 25, 0, 0);
console.log(date);
// Sat Mar 16 2030 14:25:00 GMT+0200 (Eastern European Standard Time)
Methods
A Date
class instance has many methods for reading and writing date and time values. Methods return or assign the year, month, day of the month or week, hour, minute, second and millisecond for each instance. This data can be a string based on the local calendar or language.
Getters
Getters are used to read the entire date or a single part. The return value depends on the current time zone set on your computer.
const date = new Date();
console.log("Date: ", date);
// Returns day of month from 1 to 31
console.log("getDate(): ", date.getDate());
// Returns day of week from 0 to 6
console.log("getDay(): ", date.getDay());
// Returns month from 0 to 11
console.log("getMonth(): ", date.getMonth());
// Returns 4-digit year
console.log("getFullYear(): ", date.getFullYear());
// Returns hour
console.log("getHours(): ", date.getHours());
// Returns minutes
console.log("getMinutes(): ", date.getMinutes());
// Returns seconds
console.log("getSeconds(): ", date.getSeconds());
// Returns milliseconds
console.log("getMilliseconds(): ", date.getMilliseconds());
There are equivalent versions of these methods that return Coordinated Universal Time (UTC) values rather than those adapted to the user's current time zone.
const date = new Date();
console.log("Date: ", date);
// Returns day of month from 1 to 31
console.log("getUTCDate(): ", date.getUTCDate());
// Returns day of week from 0 to 6
console.log("getUTCDay(): ", date.getUTCDay());
// Returns month from 0 to 11
console.log("getUTCMonth(): ", date.getUTCMonth());
// Returns 4-digit year
console.log("getUTCFullYear(): ", date.getUTCFullYear());
// Returns hour
console.log("getUTCHours(): ", date.getUTCHours());
// Returns minutes
console.log("getUTCMinutes(): ", date.getUTCMinutes());
// Returns seconds
console.log("getUTCSeconds(): ", date.getUTCSeconds());
// Returns milliseconds
console.log("getUTCMilliseconds(): ", date.getUTCMilliseconds());
Setters
Anything that can be read can be written; methods for writing are also called getters, but they begin with the prefix set
. Also, all methods have their UTC equivalents.
const date = new Date("March 16, 2030 14:25:00");
date.setMinutes(50);
// "Sat Mar 16 2030 14:50:00 GMT+0200"
date.setFullYear(2040, 4, 8);
// "Tue May 08 2040 14:50:00 GMT+0300"
Date formatting
Any date object can be represented in various string and numeric formats. There are various methods for this. For example, toString()
, toDateString()
and toTimeString()
return the standard string representation, not hard-coded in the standard, but browser-dependent. The only requirement is human readability. The toString()
method returns the entire date, whereas toDateString()
and toTimeString()
return only the date and time, respectively.
const date = new Date("March 16, 2030 14:25:00");
date.toString();
// "Sat Mar 16 2030 14:25:00 GMT+0200 (Eastern European Standard Time)"
date.toTimeString();
// "14:25:00 GMT+0200 (Eastern European Standard Time)"
date.toLocaleTimeString();
// "2:25:00 PM"
date.toUTCString();
// "Sat, 16 Mar 2030 12:25:00 GMT"
date.toDateString();
// "Sat Mar 16 2030"
date.toISOString();
// "2030-03-16T12:25:00.000Z"
date.toLocaleString();
// "3/16/2030, 2:25:00 PM"
date.getTime();
// 1899894300000