In JavaScript, there is a built-in object Date for handling all the date and time related operations. For instance, you can use it for displaying the current date and time, creating a calendar, build a timer, and much more.
How to Create a Date Object
To learn how to create a new Date object, follow the steps below.
Note that the new operator is generally used to create a date object.
Call new Date () choosing one of the following arguments:
new Date()
You can create a Date object for the current date and time without arguments like this:

This action returns the current date with the time and time zone.
We can distinguish other ways of creating a Date using the new operator.
New Date with Milliseconds
Creating a Date object with the time equal to the number of milliseconds passed after the January 1st, 1970 UTC+0.
Here is an example:

In this example, a timestamp is used to represent the number of milliseconds passed since the beginning of 1970.
Creating Date Object Using dateString
It is possible to create a Date object by passing the date string representation. The string representation will be accepted in case it can be parsed using Date.parse().
Here is an example:

The following format is acceptable: new Date(year, month, date, hours, minutes, seconds, ms). You should also follow these rules:
- The year should consist of four digits ( for example, 2020);
- The counting of the month must start from 0 (For example, 0 is January and 11 is December);
- The day of the month is the date parameter; if it is absent, you should assume 1.
- In case hours/minutes/seconds/ms is absent, you should assume them to be equal 0.
For example:
new Date(2019, 0, 1, 0, 0, 0, 0); // 1 Jan 2019, 00:00:00
new Date(2019, 0, 1); // the same, hours etc are 0 by default
Minimal precision should be one millisecond.
For instance:

Accessing Date Components
Now, we will discover the ways of accessing data components. Here they are:
getFullYear()
getting the year ( should be of 4 digits).
getMonth()
getting the month ( from 0 to 11).
getDate()
getting the day of a month ( from 1 to 31).
getHours(),
getMinutes(),
getSeconds(),
getMilliseconds()
getting the appropriate time components.
It is essential to use getFullYear(), not getYear(), as the latter deprecated and can return 2-digit year at times. So, you’d better never use it in your practice.
Besides, it is possible even to get a weekday by calling:
getDay()
It will allow you to get a weekday from Sunday 0 to Saturday 6.
How to Set Date Components
In JavaScript, several methods are used for setting the date and time components. Among them are:
setFullYear(year, [month], [date])
setMonth(month, [date])
setDate(date)
setHours(hour, [min], [sec], [ms])
setMinutes(min, [sec], [ms])
Some methods are capable of setting multiple components simultaneously. For instance, setHours.
Here is an example:

Autocorrection
A handy feature of Date objects is autocorrection. You can set values that are out of range, and it will adjust them automatically.
For example:

Date to Number, Date Diff
Whenever you convert a Date to number, it is transformed into the timestamp, like date.getTime().
Let’s consider the following example:

It is also important to note that dates can be subtracted. You can use it for measurements, as follows:

Date.now()
If you decide to measure time, it is not necessary to use the Date object. You can use Date.now(), which will return the current timestamp.
For instance:

Date.parse From a String
If you want to read a date from a string, the Date.parse(str) will help you. The format of the string must be: MM-DDTHH:mm:ss.sssZ. Several shorter options are also possible: YYYY-MM-DD or YYYY-MM or even YYYY.
Here is an example of the usage of Date.parse(str):

A new Date can be instantly created from the timestamp, like this:

Summary
In JavaScript, the Date object is used for representing date and time. The Date object includes both of them. As a rule, the months are counted starting from zero and ending with 11. The weekdays are also counted starting from zero. In JavaScript, the dates might be subtracted and give their difference in milliseconds, as a Date is transformed into timestamp when converted to a number. Finally, unlike other systems, in JavaScript, timestamps are in milliseconds, not seconds.