JavaScript Date Objects


JavaScript Date Object lets us work with dates:

Thu Oct 07 2021 12:13:36 GMT+0530 (India Standard Time)

Example

const d = new Date();

JavaScript Date Output

By default, JavaScript will use the browser's time zone and display a date as a full text string:

Thu Oct 07 2021 12:13:36 GMT+0530 (India Standard Time)

You will learn much more about how to display dates, later in this tutorial.


Creating Date Objects

Date objects are created with the new Date() constructor.

There are 4 ways to create a new date object:

new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)

new Date()

new Date() creates a new date object with the current date and time:

Example

const d = new Date();

Date objects are static. The computer time is ticking, but date objects are not.


new Date(year, month, ...)

new Date(year, month, ...) creates a new date object with a specified date and time.

7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):

Example

const d = new Date(2018, 11, 24, 10, 33, 30, 0);

Note: JavaScript counts months from 0 to 11:

January = 0.

December = 11.