Our volunteers haven't translated this article into Română yet. Join us and help get the job done!
You can also read the article in English (US).

Creates a JavaScript Date instance that represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC.

Syntax

new Date();
new Date(value);
new Date(dateString);
new Date(year, month [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

Note: JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.

Parameters

Note: The argument month is 0-based. This means that January = 0 and December = 11.

Note: Where Date is called as a constructor with more than one argument, if values are greater than their logical range (e.g. 13 is provided as the month value or 70 for the minute value), the adjacent value will be adjusted. E.g. new Date(2013, 13, 1) is equivalent to new Date(2014, 1, 1), both create a date for 2014-02-01 (note that the month is 0-based). Similarly for other values: new Date(2013, 2, 1, 0, 70) is equivalent to new Date(2013, 2, 1, 1, 10) which both create a date for 2013-03-01T01:10:00.

Note: Where Date is called as a constructor with more than one argument, the specifed arguments represent local time. If UTC is desired, use new Date(Date.UTC(...)) with the same arguments.

value
Integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored (Unix Epoch; but consider that most Unix timestamp functions count in seconds).
dateString
String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).

Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.

year
Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999. See the example below.
month
Integer value representing the month, beginning with 0 for January to 11 for December.
day
Optional. Integer value representing the day of the month.
hours
Optional. Integer value representing the hour of the day.
minutes
Optional. Integer value representing the minute segment of a time.
seconds
Optional. Integer value representing the second segment of a time.
milliseconds
Optional. Integer value representing the millisecond segment of a time.

Description

  • If no arguments are provided, the constructor creates a JavaScript Date object for the current date and time according to system settings.
  • If at least two arguments are supplied, missing arguments are either set to 1 (if the day is missing) or 0 for all others.
  • The JavaScript date is based on a time value that is milliseconds since midnight January 1, 1970, UTC. A day holds 86,400,000 milliseconds. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to January 1, 1970 UTC.
  • The JavaScript Date object provides uniform behavior across platforms. The time value can be passed between systems to create a date that represents the same moment in time.
  • The JavaScript Date object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.
  • Invoking JavaScript Date as a function (i.e., without the new operator) will return a string representing the current date and time.

Properties

Date.prototype
Allows the addition of properties to a JavaScript Date object.
Date.length
The value of Date.length is 7. This is the number of arguments handled by the constructor.

Methods

Date.now()
Returns the numeric value corresponding to the current time - the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC, with leap seconds ignored.
Date.parse()
Parses a string representation of a date and returns the number of milliseconds since 1 January, 1970, 00:00:00, UTC, with leap seconds ignored.

Note: Parsing of strings with Date.parse is strongly discouraged due to browser differences and inconsistencies.

Date.UTC()
Accepts the same parameters as the longest form of the constructor (i.e. 2 to 7) and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored.

JavaScript Date instances

All Date instances inherit from Date.prototype. The prototype object of the Date constructor can be modified to affect all Date instances.

Date.prototype Methods

Getter

Date.prototype.getDate()
Returns the day of the month (1-31) for the specified date according to local time.
Date.prototype.getDay()
Returns the day of the week (0-6) for the specified date according to local time.
Date.prototype.getFullYear()
Returns the year (4 digits for 4-digit years) of the specified date according to local time.
Date.prototype.getHours()
Returns the hour (0-23) in the specified date according to local time.
Date.prototype.getMilliseconds()
Returns the milliseconds (0-999) in the specified date according to local time.
Date.prototype.getMinutes()
Returns the minutes (0-59) in the specified date according to local time.
Date.prototype.getMonth()
Returns the month (0-11) in the specified date according to local time.
Date.prototype.getSeconds()
Returns the seconds (0-59) in the specified date according to local time.
Date.prototype.getTime()
Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative for prior times).
Date.prototype.getTimezoneOffset()
Returns the time-zone offset in minutes for the current locale.
Date.prototype.getUTCDate()
Returns the day (date) of the month (1-31) in the specified date according to universal time.
Date.prototype.getUTCDay()
Returns the day of the week (0-6) in the specified date according to universal time.
Date.prototype.getUTCFullYear()
Returns the year (4 digits for 4-digit years) in the specified date according to universal time.
Date.prototype.getUTCHours()
Returns the hours (0-23) in the specified date according to universal time.
Date.prototype.getUTCMilliseconds()
Returns the milliseconds (0-999) in the specified date according to universal time.
Date.prototype.getUTCMinutes()
Returns the minutes (0-59) in the specified date according to universal time.
Date.prototype.getUTCMonth()
Returns the month (0-11) in the specified date according to universal time.
Date.prototype.getUTCSeconds()
Returns the seconds (0-59) in the specified date according to universal time.
Date.prototype.getYear()
Returns the year (usually 2-3 digits) in the specified date according to local time. Use getFullYear() instead.

Setter

Date.prototype.setDate()
Sets the day of the month for a specified date according to local time.
Date.prototype.setFullYear()
Sets the full year (e.g. 4 digits for 4-digit years) for a specified date according to local time.
Date.prototype.setHours()
Sets the hours for a specified date according to local time.
Date.prototype.setMilliseconds()
Sets the milliseconds for a specified date according to local time.
Date.prototype.setMinutes()
Sets the minutes for a specified date according to local time.
Date.prototype.setMonth()
Sets the month for a specified date according to local time.
Date.prototype.setSeconds()
Sets the seconds for a specified date according to local time.
Date.prototype.setTime()
Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC, allowing for negative numbers for times prior.
Date.prototype.setUTCDate()
Sets the day of the month for a specified date according to universal time.
Date.prototype.setUTCFullYear()
Sets the full year (e.g. 4 digits for 4-digit years) for a specified date according to universal time.
Date.prototype.setUTCHours()
Sets the hour for a specified date according to universal time.
Date.prototype.setUTCMilliseconds()
Sets the milliseconds for a specified date according to universal time.
Date.prototype.setUTCMinutes()
Sets the minutes for a specified date according to universal time.
Date.prototype.setUTCMonth()
Sets the month for a specified date according to universal time.
Date.prototype.setUTCSeconds()
Sets the seconds for a specified date according to universal time.
Date.prototype.setYear()
Sets the year (usually 2-3 digits) for a specified date according to local time. Use setFullYear() instead.

Conversion getter

Date.prototype.toDateString()
Returns the "date" portion of the Date as a human-readable string.
Date.prototype.toISOString()
Converts a date to a string following the ISO 8601 Extended Format.
Date.prototype.toJSON()
Returns a string representing the Date using toISOString(). Intended for use by JSON.stringify().
Date.prototype.toGMTString()
Returns a string representing the Date based on the GMT (UT) time zone. Use toUTCString() instead.
Date.prototype.toLocaleDateString()
Returns a string with a locality sensitive representation of the date portion of this date based on system settings.
Date.prototype.toLocaleFormat()
Converts a date to a string, using a format string.
Date.prototype.toLocaleString()
Returns a string with a locality sensitive representation of this date. Overrides the Object.prototype.toLocaleString() method.
Date.prototype.toLocaleTimeString()
Returns a string with a locality sensitive representation of the time portion of this date based on system settings.
Date.prototype.toSource()
Returns a string representing the source for an equivalent Date object; you can use this value to create a new object. Overrides the Object.prototype.toSource() method.
Date.prototype.toString()
Returns a string representing the specified Date object. Overrides the Object.prototype.toString() method.
Date.prototype.toTimeString()
Returns the "time" portion of the Date as a human-readable string.
Date.prototype.toUTCString()
Converts a date to a string using the UTC timezone.
Date.prototype.valueOf()
Returns the primitive value of a Date object. Overrides the Object.prototype.valueOf() method.

Examples

Several ways to create a Date object

The following examples show several ways to create JavaScript dates:

Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);

Two digit years map to 1900 - 1999

In order to create and get dates between the years 0 and 99 the Date.prototype.setFullYear() and Date.prototype.getFullYear() methods should be used.

var date = new Date(98, 1); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)

// Deprecated method, 98 maps to 1998 here as well
date.setYear(98);           // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)

date.setFullYear(98);       // Sat Feb 01 0098 00:00:00 GMT+0000 (BST)

Calculating elapsed time

The following examples show how to determine the elapsed time between two JavaScript dates in milliseconds.

Due to the differing lengths of days (due to daylight saving changeover), months and years, expressing elapsed time in units greater than hours, minutes and seconds requires addressing a number of issues and should be thoroughly researched before being attempted.

// using Date objects
var start = Date.now();

// the event to time goes here:
doSomethingForALongTime();
var end = Date.now();
var elapsed = end - start; // elapsed time in milliseconds
// using built-in methods
var start = new Date();

// the event to time goes here:
doSomethingForALongTime();
var end = new Date();
var elapsed = end.getTime() - start.getTime(); // elapsed time in milliseconds
// to test a function and get back its return
function printElapsedTime(fTest) {
  var nStartTime = Date.now(),
      vReturn = fTest(),
      nEndTime = Date.now();

  console.log('Elapsed time: ' + String(nEndTime - nStartTime) + ' milliseconds');
  return vReturn;
}

var yourFunctionReturn = printElapsedTime(yourFunction);

Note: In browsers that support the Web Performance API's high-resolution time feature, Performance.now() can provide more reliable and precise measurements of elapsed time than Date.now().

Get the number of seconds since Unix Epoch

var seconds = Math.floor(Date.now() / 1000);

In this case it's important to return only a whole number (so a simple division won't do), and also to only return actually elapsed seconds (that's why this code uses Math.floor() and not Math.round()).

Specifications

Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'Date' in that specification.
Draft  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Date' in that specification.
Standard  
ECMAScript 5.1 (ECMA-262)
The definition of 'Date' in that specification.
Standard  
ECMAScript 1st Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.1.

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
@@toPrimitive ? ?44 ? ? ?
Date Yes Yes1 Yes1 Yes Yes
UTC Yes Yes1 Yes Yes Yes
getDate Yes Yes1 Yes Yes Yes
getDay Yes Yes1 Yes Yes Yes
getFullYear Yes Yes1 Yes Yes Yes
getHours Yes Yes1 Yes Yes Yes
getMilliseconds Yes Yes1 Yes Yes Yes
getMinutes Yes Yes1 Yes Yes Yes
getMonth Yes Yes1 Yes Yes Yes
getSeconds Yes Yes1 Yes Yes Yes
getTime Yes Yes1 Yes Yes Yes
getTimezoneOffset Yes Yes15 Yes Yes
getUTCDate Yes Yes1 Yes Yes Yes
getUTCDay Yes Yes1 Yes Yes Yes
getUTCFullYear Yes Yes1 Yes Yes Yes
getUTCHours Yes Yes1 Yes Yes Yes
getUTCMilliseconds Yes Yes1 Yes Yes Yes
getUTCMinutes Yes Yes1 Yes Yes Yes
getUTCMonth Yes Yes1 Yes Yes Yes
getUTCSeconds Yes Yes1 Yes Yes Yes
getYear Yes Yes1 Yes Yes Yes
now5 Yes3910.54
parse Yes Yes1 Yes Yes Yes
prototype Yes Yes1 Yes Yes Yes
setDate Yes Yes1 Yes Yes Yes
setFullYear Yes Yes1 Yes Yes Yes
setHours Yes Yes1 Yes Yes Yes
setMilliseconds Yes Yes1 Yes Yes Yes
setMinutes Yes Yes1 Yes Yes Yes
setMonth Yes Yes1 Yes Yes Yes
setSeconds Yes Yes1 Yes Yes Yes
setTime Yes Yes1 Yes Yes Yes
setUTCDate Yes Yes1 Yes Yes Yes
setUTCFullYear Yes Yes1 Yes Yes Yes
setUTCHours Yes Yes1 Yes Yes Yes
setUTCMilliseconds Yes Yes1 Yes Yes Yes
setUTCMinutes Yes Yes1 Yes Yes Yes
setUTCMonth Yes Yes1 Yes Yes Yes
setUTCSeconds Yes Yes1 Yes Yes Yes
setYear Yes Yes1 Yes Yes Yes
toDateString Yes Yes1 Yes Yes Yes
toGMTString Yes Yes1 Yes Yes Yes
toISOString Yes Yes19 Yes Yes
toJSON Yes Yes18 Yes Yes
toLocaleDateString Yes Yes1 Yes Yes Yes
toLocaleFormat No No1.5 — 58 No No No
toLocaleString Yes Yes1 Yes Yes Yes
toLocaleTimeString Yes Yes1 Yes Yes Yes
toSource No No1 No No No
toString Yes Yes1 Yes Yes Yes
toTimeString Yes Yes1 Yes Yes Yes
toUTCString Yes Yes1 Yes Yes Yes
valueOf Yes Yes1 Yes Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
@@toPrimitive ? ? ?44 ? ? ?
Date Yes Yes Yes4 Yes Yes ?
UTC Yes Yes Yes4 Yes Yes ?
getDate Yes Yes Yes4 Yes Yes ?
getDay Yes Yes Yes4 Yes Yes ?
getFullYear Yes Yes Yes4 Yes Yes ?
getHours Yes Yes Yes4 Yes Yes ?
getMilliseconds Yes Yes Yes4 Yes Yes ?
getMinutes Yes Yes Yes4 Yes Yes ?
getMonth Yes Yes Yes4 Yes Yes ?
getSeconds Yes Yes Yes4 Yes Yes ?
getTime Yes Yes Yes4 Yes Yes ?
getTimezoneOffset Yes Yes Yes4 Yes Yes ?
getUTCDate Yes Yes Yes4 Yes Yes ?
getUTCDay Yes Yes Yes4 Yes Yes ?
getUTCFullYear Yes Yes Yes4 Yes Yes ?
getUTCHours Yes Yes Yes4 Yes Yes ?
getUTCMilliseconds Yes Yes Yes4 Yes Yes ?
getUTCMinutes Yes Yes Yes4 Yes Yes ?
getUTCMonth Yes Yes Yes4 Yes Yes ?
getUTCSeconds Yes Yes Yes4 Yes Yes ?
getYear Yes Yes Yes4 Yes Yes ?
now Yes Yes Yes4 Yes Yes ?
parse Yes Yes Yes4 Yes Yes ?
prototype Yes Yes Yes4 Yes Yes ?
setDate Yes Yes Yes4 Yes Yes ?
setFullYear Yes Yes Yes4 Yes Yes ?
setHours Yes Yes Yes4 Yes Yes ?
setMilliseconds Yes Yes Yes4 Yes Yes ?
setMinutes Yes Yes Yes4 Yes Yes ?
setMonth Yes Yes Yes4 Yes Yes ?
setSeconds Yes Yes Yes4 Yes Yes ?
setTime Yes Yes Yes4 Yes Yes ?
setUTCDate Yes Yes Yes4 Yes Yes ?
setUTCFullYear Yes Yes Yes4 Yes Yes ?
setUTCHours Yes Yes Yes4 Yes Yes ?
setUTCMilliseconds Yes Yes Yes4 Yes Yes ?
setUTCMinutes Yes Yes Yes4 Yes Yes ?
setUTCMonth Yes Yes Yes4 Yes Yes ?
setUTCSeconds Yes Yes Yes4 Yes Yes ?
setYear Yes Yes Yes4 Yes Yes ?
toDateString Yes Yes Yes4 Yes Yes ?
toGMTString Yes Yes Yes4 Yes Yes ?
toISOString Yes Yes Yes4 Yes Yes ?
toJSON Yes Yes Yes4 Yes Yes ?
toLocaleDateString Yes Yes Yes4 Yes Yes ?
toLocaleFormat No No No4 — 58 No No ?
toLocaleString Yes Yes Yes4 Yes Yes ?
toLocaleTimeString Yes Yes Yes4 Yes Yes ?
toSource No No No4 No No ?
toString Yes Yes Yes4 Yes Yes ?
toTimeString Yes Yes Yes4 Yes Yes ?
toUTCString Yes Yes Yes4 Yes Yes ?
valueOf Yes Yes Yes4 Yes Yes ?

1. The ISO8601 Date Format is not supported in Internet Explorer 8.