Table of contents
Summary
Creates JavaScript Date instances which let you work with dates and times.
Syntax
new Date() new Date(milliseconds) new Date(dateString) new Date(year, month, day [, hour, minute, second, millisecond ])
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
milliseconds- Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).
dateString- String value representing a date. The string should be in a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).
year- Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use
1998, rather than98.
month- Integer value representing the month, beginning with 0 for January to 11 for December.
day- Integer value representing the day of the month (1-31).
hour- Integer value representing the hour of the day (0-23).
minute- Integer value representing the minute segment (0-59) of a time reading.
second- Integer value representing the second segment (0-59) of a time reading.
millisecond- Integer value representing the millisecond segment (0-999) of a time reading.
Description
If you supply no arguments, the constructor creates a JavaScript Date object for today's date and time according to local time. If you supply some arguments but not others, the missing arguments are set to 0. If you supply any arguments, you must supply at least the year, month, and day. You can omit the hours, minutes, seconds, and milliseconds.
The JavaScript date is measured in milliseconds since midnight 01 January, 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 01 January, 1970 UTC.
The JavaScript Date object provides uniform behavior across platforms.
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 in a non-constructor context (i.e., without the new operator) will return a string representing the current time.
Properties
For properties available on Date instances, see Properties of Date instances.
- prototype
- Allows the addition of properties to a JavaScript
Dateobject.
Methods
For methods available on Date instances, see Methods of Date instances.
- now
- Returns the numeric value corresponding to the current time.
- parse
- Parses a string representation of a JavaScript date, and returns the number of milliseconds since January 1, 1970, 00:00:00, local time.
- UTC
- Accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a JavaScript
Dateobject since January 1, 1970, 00:00:00, universal time.
JavaScript Date instances
Date instances inherit from Date.prototype . You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances.
For compatibility with millennium calculations (in other words, to take into account the year 2000), you should always specify the year in full; for example, use 1998, not 98. To assist you in specifying the complete year, JavaScript includes the methods getFullYear, setFullYear, getUTCFullYear, and setUTCFullYear.
Properties
- constructor
- Returns the function that created an instance. This is the
Dateconstructor by default.
Methods
- getDate
- Returns the day of the month (1-31) for the specified date according to local time.
- getDay
- Returns the day of the week (0-6) for the specified date according to local time.
- getFullYear
- Returns the year (4 digits for 4-digit years) of the specified date according to local time.
- getHours
- Returns the hour (0-23) in the specified date according to local time.
- getMilliseconds
- Returns the milliseconds (0-999) in the specified date according to local time.
- getMinutes
- Returns the minutes (0-59) in the specified date according to local time.
- getMonth
- Returns the month (0-11) in the specified date according to local time.
- getSeconds
- Returns the seconds (0-59) in the specified date according to local time.
- 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).
- getTimezoneOffset
- Returns the time-zone offset in minutes for the current locale.
- getUTCDate
- Returns the day (date) of the month (1-31) in the specified date according to universal time.
- getUTCDay
- Returns the day of the week (0-6) in the specified date according to universal time.
- getUTCFullYear
- Returns the year (4 digits for 4-digit years) in the specified date according to universal time.
- getUTCHours
- Returns the hours (0-23) in the specified date according to universal time.
- getUTCMilliseconds
- Returns the milliseconds (0-999) in the specified date according to universal time.
- getUTCMinutes
- Returns the minutes (0-59) in the specified date according to universal time.
- getUTCMonth
- Returns the month (0-11) in the specified date according to universal time.
- getUTCSeconds
- Returns the seconds (0-59) in the specified date according to universal time.
- getYear
- Deprecated
- Returns the year (usually 2-3 digits) in the specified date according to local time. Use getFullYear instead.
- setDate
- Sets the day of the month (1-31) for a specified date according to local time.
- setFullYear
- Sets the full year (4 digits for 4-digit years) for a specified date according to local time.
- setHours
- Sets the hours (0-23) for a specified date according to local time.
- setMilliseconds
- Sets the milliseconds (0-999) for a specified date according to local time.
- setMinutes
- Sets the minutes (0-59) for a specified date according to local time.
- setMonth
- Sets the month (0-11) for a specified date according to local time.
- setSeconds
- Sets the seconds (0-59) for a specified date according to local time.
- setTime
- Sets the
Dateobject to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC, allowing for negative numbers for times prior.
- setUTCDate
- Sets the day of the month (1-31) for a specified date according to universal time.
- setUTCFullYear
- Sets the full year (4 digits for 4-digit years) for a specified date according to universal time.
- setUTCHours
- Sets the hour (0-23) for a specified date according to universal time.
- setUTCMilliseconds
- Sets the milliseconds (0-999) for a specified date according to universal time.
- setUTCMinutes
- Sets the minutes (0-59) for a specified date according to universal time.
- setUTCMonth
- Sets the month (0-11) for a specified date according to universal time.
- setUTCSeconds
- Sets the seconds (0-59) for a specified date according to universal time.
- setYear
- Deprecated
- Sets the year (usually 2-3 digits) for a specified date according to local time. Use setFullYear instead.
- toDateString
- Returns the "date" portion of the Date as a human-readable string.
- toISOString
- Converts a date to a string following the ISO 8601 Extended Format.
- toJSON Requires JavaScript 1.8.5
- Returns a string encapsulating the Date object in JSON format.
- toGMTString
- Deprecated
- Converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.
- toLocaleDateString
- Returns the "date" portion of the Date as a string, using the current locale's conventions.
- toLocaleFormat
- Non-standard
- Converts a date to a string, using a format string.
- toLocaleString
- Converts a date to a string, using the current locale's conventions. Overrides the Object.toLocaleString method.
- toLocaleTimeString
- Returns the "time" portion of the Date as a string, using the current locale's conventions.
- toSource
- Non-standard
- Returns a string representing the source for an equivalent
Dateobject; you can use this value to create a new object. Overrides the Object.prototype.toSource method.
- toString
- Returns a string representing the specified
Dateobject. Overrides the Object.prototype.toString method.
- toTimeString
- Returns the "time" portion of the Date as a human-readable string.
- toUTCString
- Converts a date to a string, using the universal time convention.
- valueOf
- Returns the primitive value of a
Dateobject. Overrides the Object.prototype.valueOf method.
Object:__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch
Examples
Example: Several ways to assign dates
The following examples show several ways to assign JavaScript dates:
today = new Date();
birthday = new Date("December 17, 1995 03:24:00");
birthday = new Date(1995,11,17);
birthday = new Date(1995,11,17,3,24,0);
Example: Calculating elapsed time
The following examples show how to determine the elapsed time between two JavaScript dates:
// using static methods var start = Date.now(); // the event you'd like to time goes here: doSomethingForALongTime(); var end = Date.now(); var elapsed = end - start; // time in milliseconds
// if you have Date objects var start = new Date(); // the event you'd like to time goes here: doSomethingForALongTime(); var end = new Date(); var elapsed = end.getTime() - start.getTime(); // time in milliseconds
// if you want to test a function and get back its return
function printElapsedTime (fTest) {
var nStartTime = Date.now(), vReturn = fTest(), nEndTime = Date.now();
alert("Elapsed time: " + String(nEndTime - nStartTime) + " milliseconds");
return vReturn;
}
yourFunctionReturn = printElapsedTime(yourFunction);
Example: ISO 8601 formatted dates
Date.toISOString() is now supported, so you can use that.
The following example demonstrates how to manually format a JavaScript date, in an ISO 8601 format using UTC:
/* use a function for the exact format desired... */
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'
}
var d = new Date();
console.log(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z


Mozilla Developer Network