Learn JavaScript
Date
29.1
Lesson

Date object

The Date object stores the date as the number of milliseconds passed after the midnight of January 1, 1970.

We can use the new Date() function to create a Date object. It creates a Date object which stores the current date and time.

The new Date(milliseconds) function creates the Date object with the number of milliseconds passed after January 1, 1970.

In the example given below, we are using 1609459200000 as the argument. It is the number of milliseconds passed till January 01, 2021.

The new Date(dateString) function creates the Date object from a string. The string represents the date in a specific format.

In the example given below, the string contains the date in the YYYY-MM-DD format.

In the example given below, the string contains the date in the Month DD, YYYY format.

We can specify the value for year, month, day, hour, minutes, seconds and milliseconds in the new Date() function.

The year and month arguments are compulsory. The remaining arguments are optional. They are assigned a default value if omitted.

The day defaults to 1, while hours, minutes, seconds and milliseconds defaults to 0.