JavaScript Date Objects


JavaScript Date Objects let us work with dates:

Note : Date objects are static. The clock is not running.

The computer's clock is counting time, but date objects aren't.


JavaScript Date Output

JavaScript typically uses the time zone of the user's web browser and shows a date as a complete text description.

Today is Monday, October 30, 2023, and it's currently 2:37 PM in Pakistan. This message is written in bold.


Creating Date Objects

You can create Date objects using the new Date() constructor.

There are 9 ways to create a new date object:

new Date()
new Date(date string)

new Date(year,month)
new Date(year,month,day)
new Date(year,month,day,hours)
new Date(year,month,day,hours,minutes)
new Date(year,month,day,hours,minutes,seconds)
new Date(year,month,day,hours,minutes,seconds,ms)

new Date(milliseconds)

JavaScript new Date()

The code new Date() makes a new date and time object that represents the current date and time.


new Date(date string)

The code new Date(date string) makes a date object from a date string.


new Date(year, month, ...)

The HTML code new Date(year, month, ...) generates a date object with a specific date and time.

Seven numbers are used to indicate the year, month, day, hour, minute, second, and millisecond in that exact order.

Note : JavaScript counts months from 0 to 11:

January = 0.

December = 11.

Setting a month value greater than 11 won't cause an error, but it will roll over to the following year.

If you choose a day greater than the maximum allowed, it won't cause an error. Instead, the extra days will be counted in the following month.


Using 6, 4, 3, or 2 Numbers

Use six numbers to indicate the year, month, day, hour, minute, and second.

5 numbers specify year, month, day, hour, and minute:

4 numbers specify year, month, day, and hour:

3 numbers tell us the year, month, and day.

2 numbers indicate the year and month:

You cannot omit month. If you provide just one parameter, it will be considered as milliseconds.


Previous Century

Years with one or two digits will be understood as in the 1900s.


JavaScript Stores Dates as Milliseconds

JavaScript counts dates as the number of milliseconds from January 01, 1970.

The starting point is January 01, 1970 00:00:00 UTC.

One full day, which is 24 hours, equals 86,400,000 milliseconds.

Now the time is: 1698658668650 milliseconds past January 01, 1970


new Date(milliseconds)

The code new Date(milliseconds) generates a fresh date object based on the provided number of milliseconds, adding no additional time.


Date Methods

When you create a date object, several functions are available for you to work with it.

Date methods let you access and change different parts of a date, like the year, month, day, hour, minute, second, and even milliseconds. You can do this using either the time based on your local time zone or the universal time (UTC/GMT).


Displaying Dates

JavaScript will display dates using the toString() method as a default. This shows the date as a text, along with the time zone information. The specific format is defined in the ECMAScript specification.

When you show a date in HTML, it turns into text using the toString() method.

The toDateString() function changes a date to a simpler, easier-to-read format.

The toUTCString() function changes a date into a string, following the UTC standard.

The toISOString() function changes a date into a text using the ISO format.