Dates

Date objects in JavaScript represent a single moment in time stored as a number that represents the number of milliseconds since midnight on January 1st 1970 UTC. Note that this is different from the UNIX epoch, the number of seconds since midnight on January 1st 1970 UTC.

Also note that, even though the representation of a Date is in UTC, the methods to fetch a date or its components work in the host system’s local time zone.

The defined range for a Date value is between April 20, 271821 BCE to September 13, 275760 CE.

Syntax

When used as a function, Date() returns the current date and time. When used as a constructor, Date() returns a new date object.

Loading...

The date constructor can take several different parameters to create a new Date object.

var d = new Date();

  • No parameter creates Date object with current date.

var d = new Date(milliseconds);

  • milliseconds creates Date object based on milliseconds from midnight 1/1/1970 UTC.

var d = new Date(dateString);

  • dateString creates a Date object based on converting the sting to a date value.

var d = new Date(year, month, day, hour, minute, second, millisecond);

  • Or you can specify the date referred to by the Date object by specifying the numeric value for each component of the date.

Date Methods

Static Methods

Method Description
.now() Returns the number of milliseconds elapsed since midnight 1/1/1970 UTC.
.parse() Parses a string representation of a date and returns the number of milliseconds represented by that date.

The instance methods are listed at the bottom of the page.

Examples

Loading...

Dates

.UTC()
Returns a number value representing the number of milliseconds between the specified date and January 1, 1970, 00:00:00, Universal Time Coordinated. Will always be called as Date.UTC() rather than called on an instance of date such as myDate.UTC().
.getDate()
Called from an instance of the Date class, will return the date of the month. All return values will be integers between 1 and 31.
.getDay()
Called from an instance of the Date class, will return the day of the week.
.getHours()
Called from an instance of the Date class, will return the hour according to the local time.
.getMinutes()
Called from an instance of the Date class, will return the minutes according to the local time.
.getMonth()
Called from an instance of the Date class, will return the month of the year.
.getSeconds()
Called from an instance of the Date class, will return the seconds according to the local time.
.now()
Returns the current time in milliseconds. The milliseconds count begins at 1970/01/01 at 00:00:00 UTC.
.toString()
Converts a Date object to a string.
.valueOf()
Returns the difference in milliseconds between the specified date and January 1, 1970 00:00:00 UTC.
Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn JavaScript on Codecademy