--- title: Date slug: Web/JavaScript/Reference/Global_Objects/Date tags: - Date - Epoch - JavaScript - NeedsTranslation - Time - TopicStub - Unix Epoch - timeStamp translation_of: Web/JavaScript/Reference/Global_Objects/Date ---
{{JSRef}}

Creates a JavaScript Date instance that represents a single moment in time in a platform-independent format. Date objects use a Unix Time Stamp, an integer value that is the number of milliseconds since 1 January 1970 UTC.

{{EmbedInteractiveExample("pages/js/date-constructor.html")}}

Instantiating Date objects

The only way to create a new JavaScript Date object is to use the {{jsxref("new")}} operator:

let now = new Date();

If you simply call the {{jsxref("Date", "Date()")}} object directly, the returned value is a string instead of a Date object. There's no Date literal syntax in JavaScript.

Syntax

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

Parameters

There are four basic forms for the Date() constructor:

No parameters

When no parameters are provided, the newly-created Date object represents the current date and time, specified in the local time zone, as of the time of instantiation.

Unix timestamp

value
A Unix Time Stamp which is an integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC (the Unix epoch), with leap seconds ignored. Keep in mind that most Unix timestamp functions are only accurate to the nearest second.

Timestamp string

dateString
A string value representing a date, specified in a format recognized by the {{jsxref("Date.parse()")}} method (these formats are IETF-compliant RFC 2822 timestamps and also strings in 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.

Individual date and time component values

Given at least a year and month, this form of Date() returns a Date object whose component values (year, month, day, hour, minute, second, and millisecond) all come from the following parameters. Any missing fields are given the lowest possible value (1 for the day and 0 for every other component).

year
Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999; all other values are the actual year. See the {{anch("Two_digit_years_map_to_1900_-_1999", "example below")}}.
monthIndex
Integer value representing the month, beginning with 0 for January to 11 for December.
day {{optional_inline}}
Integer value representing the day of the month. If not specified, the default value of 1 is used.
hours {{optional_inline}}
Integer value representing the hour of the day. The default is 0 (midnight).
minutes {{optional_inline}}
Integer value representing the minute segment of a time. The default is 0 minutes past the hour.
seconds {{optional_inline}}
Integer value representing the second segment of a time. The default is zero seconds past the minute.
milliseconds {{optional_inline}}
Integer value representing the millisecond segment of a time. The default is 0 milliseconds past the second.

User notes

The Unix epoch and timestamps

A JavaScript date is fundamentally specified as the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC. This date and time is called the Unix epoch, which is the predominant base value for computer-recorded date and time values.

Note: It's important to keep in mind that the date and time is stored in the local time zone, and that the basic methods to fetch the date and time or its components all work in the local time zone as well.

A day is made up of 86,400,000 milliseconds. Given that and the size of the underlying number used to record the timestamp, and it can be calculated that the Date object can represent dates within ±100,000,000 (one hundred million) days relative to January 1, 1970 UTC. That means that in the year 293,742, we'll have to think about fixing this.

Date format and time zone conversions

There are a number of methods available to obtain the date in various formats, as well as to do time zone conversions. Especially useful are the functions that output the date and time in Coordinated Universal Time (UTC), the global standard time defined by the World Time Standard. This time is historically known as Greenwich Mean Time, as UTC lies along the meridian that includes London and nearby Greenwhich in the United Kingdom. The user's device provides the local time.

In addition to methods to read and alter individual components of the local date and time, such as {{jsxref("Date.getDay", "getDay()")}} and {{jsxref("Date.setHours", "setHours()")}}, there are also versions of the same methods that read and maniuplate the date and time using UTC, such as {{jsxref("Date.getUTCDay()", "getUTCDay()")}} and {{jsxref("Date.setHoursUTC", "setUTCHours()")}}.

Properties

{{jsxref("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. It's not generally a useful result.

Methods

{{jsxref("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.
{{jsxref("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.

{{jsxref("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 {{jsxref("Date.prototype")}}. The prototype object of the Date constructor can be modified to affect all Date instances.

Date.prototype Methods

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/prototype', 'Methods')}}

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 {{jsxref("Date.prototype.setFullYear()")}} and {{jsxref("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 {{domxref("window.performance", "Web Performance API", "", 1)}}'s high-resolution time feature, {{domxref("Performance.now()")}} can provide more reliable and precise measurements of elapsed time than {{jsxref("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 an integer (so a simple division won't do), and also to only return actually elapsed seconds (that's why this code uses {{jsxref("Math.floor()")}} and not {{jsxref("Math.round()")}}).

Specifications

Specification Status Comment
{{SpecName('ESDraft', '#sec-date-objects', 'Date')}} {{Spec2('ESDraft')}}  
{{SpecName('ES6', '#sec-date-objects', 'Date')}} {{Spec2('ES6')}}  
{{SpecName('ES5.1', '#sec-15.9', 'Date')}} {{Spec2('ES5.1')}}  
{{SpecName('ES1')}} {{Spec2('ES1')}} Initial definition. Implemented in JavaScript 1.1.

Browser compatibility

{{Compat("javascript.builtins.Date", 3)}}