The Date object lets you work with dates (years, months, days, minutes, seconds, milliseconds)
In this tutorial we use a script to display dates inside a <p> element with id="demo":
The script above says: assign the value of Date() to the content (innerHTML) of the element with id="demo".
![]() |
You will learn how to display a date, in a more readable format, at the bottom of this page. |
---|
The Date object lets us work with dates.
A date consists of a year, a month, a day, a minute, a second, and a millisecond.
Date objects are created with the new Date() constructor.
There are 4 ways of initiating a date:
Using new Date(), creates a new date object with the current date and time:
Using new Date(date string), creates a new date object from the specified date and time:
Using new Date(number), creates a new date object as zero time plus the number.
Zero time is 01 January 1970 00:00:00 UTC. The number is specified in milliseconds:
![]() |
JavaScript dates are calculated in milliseconds from 01 January, 1970
00:00:00 Universal Time (UTC). One day contains 86,400,000 millisecond. |
---|
Using new Date(7 numbers), creates a new date object with the specified date and time:
The 7 numbers specify the year, month, day, hour, minute, second, and millisecond, in that order:
Variants of the example above let us omit any of the last 4 parameters:
![]() |
JavaScript counts months from 0 to 11. January is 0. December is 11. |
---|
When a Date object is created, a number of methods allow you to operate on it.
Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of objects, using either local time or UTC (universal, or GMT) time.
The next chapter, of this tutorial, covers the date object's methods.
When you display a date object in HTML, it is automatically converted to a string, with the toString() method.
Is the same as:
The toUTCString() method converts a date to a UTC string (a date display standard).
The toDateString() method converts a date to a more readable format:
![]() |
Date objects are static, not dynamic. The computer time is ticking, but date objects, once created, are not. |
---|