Date Object (JavaScript)

Enables basic storage and retrieval of dates and times.

Syntax


      dateObj = new Date()  
dateObj = new Date(dateVal)  
dateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])   

Parameters

dateObj
Required. The variable name to which the Date object is assigned.

dateVal
Required. If a numeric value, dateVal represents the number of milliseconds in Universal Coordinated Time between the specified date and midnight January 1, 1970. If a string, dateVal is parsed according to the rules in Date and Time Strings. The dateVal argument can also be a VT_DATE value as returned from some ActiveX objects.

year
Required. The full year, for example, 1976 (and not 76).

month
Required. The month as an integer between 0 and 11 (January to December).

date
Required. The date as an integer between 1 and 31.

hours
Optional. Must be supplied if minutes is supplied. An integer from 0 to 23 (midnight to 11pm) that specifies the hour.

minutes
Optional. Must be supplied if seconds is supplied. An integer from 0 to 59 that specifies the minutes.

seconds
Optional. Must be supplied if milliseconds is supplied. An integer from 0 to 59 that specifies the seconds.

ms
Optional. An integer from 0 to 999 that specifies the milliseconds.

Remarks

A Date object contains a number representing a particular instant in time to within a millisecond. If the value of an argument is greater than its range or is a negative number, other stored values are modified accordingly. For example, if you specify 150 seconds, JavaScript redefines that number as two minutes and 30 seconds.

If the number is NaN, the object does not represent a specific instant of time. If you pass no parameters to the Date object, it is initialized to the current time (UTC). A value must be given to the object before you can use it.

The range of dates that can be represented in a Date object is approximately 285,616 years on either side of January 1, 1970.

See Calculating Dates and Times (JavaScript) for more information about how to use the Date object and related methods.

Example

The following example illustrates the use of the Date object.

var dateString = "Today's date is: ";  

var newDate = new Date();  

// Get the month, day, and year.  
dateString += (newDate.getMonth() + 1) + "/";  
dateString += newDate.getDate() + "/";  
dateString += newDate.getFullYear();  

document.write(dateString);  

// Output: Today's date is: <date>  

Requirements

The Date object was introduced in Internet Explorer before Internet Explorer 6. Some members in the following lists were introduced in later versions. For more information, see Version Information or the documentation for the individual members.

Properties

The following table lists properties of the Date Object.

Property Description
constructor Property Specifies the function that creates an object.
prototype Property Returns a reference to the prototype for a class of objects.

Functions

The following table lists functions of the Date Object.

Functions Description
Date.now Function Returns the number of milliseconds between January 1, 1970, and the current date and time.
Date.parse Function Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.
Date.UTC Function Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the supplied date.

Methods

The following table lists methods of the Date Object.

Method Description
getDate Method Returns the day-of-the-month value using local time.
getDay Method Returns the day-of-the-week value using local time.
getFullYear Method Returns the year value using local time.
getHours Method Returns the hours value using local time.
getMilliseconds Method Returns the milliseconds value using local time.
getMinutes Method Returns the minutes value using local time.
getMonth Method Returns the month value using local time.
getSeconds Method Returns seconds value using local time.
getTime Method Returns the time value in a Date Object as the number of milliseconds since midnight January 1, 1970.
getTimezoneOffset Method Returns the difference in minutes between the time on the host computer and Universal Coordinated Time (UTC).
getUTCDate Method Returns the day-of-the-month value using UTC.
getUTCDay Method Returns the day-of-the-week value using UTC.
getUTCFullYear Method Returns the year value using UTC.
getUTCHours Method Returns the hours value using UTC.
getUTCMilliseconds Method Returns the milliseconds value using UTC.
getUTCMinutes Method Returns the minutes value using UTC.
getUTCMonth Method Returns the month value using UTC.
getUTCSeconds Method Returns the seconds value using UTC.
getVarDate Method Returns the VT_DATE value in a Date object.
getYear Method Returns the year value .
hasOwnProperty Method Returns a Boolean value that indicates whether an object has a property with the specified name.
isPrototypeOf Method Returns a Boolean value that indicates whether an object exists in another object's prototype chain.
propertyIsEnumerable Method Returns a Boolean value that indicates whether a specified property is part of an object and whether it is enumerable.
setDate Method Sets the numeric day of the month using local time.
setFullYear Method Sets the year value using local time.
setHours Method Sets the hours value using local time.
setMilliseconds Method Sets the milliseconds value using local time.
setMinutes Method Sets the minutes value using local time.
setMonth Method Sets the month value using local time.
setSeconds Method Sets the seconds value using local time.
setTime Method Sets the date and time value in the Date object.
setUTCDate Method Sets the numeric day of the month using UTC.
setUTCFullYear Method Sets the year value using UTC.
setUTCHours Method Sets the hours value using UTC.
setUTCMilliseconds Method Sets the milliseconds value using UTC.
setUTCMinutes Method Sets the minutes value using UTC.
setUTCMonth Method Sets the month value using UTC.
setUTCSeconds Method Sets the seconds value using UTC.
setYear Method Sets the year value using local time.
toDateString Method Returns a date as a string value.
toGMTString Method Returns a date converted to a string using Greenwich Mean Time (GMT).
toISOString Method Returns a date as a string value in ISO format.
toJSON Method Used to transform data of an object type before the JSON serialization.
toLocaleDateString Method Returns a date as a string value appropriate to the host environment's current locale.
toLocaleString Method Returns an object converted to a string using the current locale.
toLocaleTimeString Method Returns a time as a string value appropriate to the host environment's current locale.
toString Method Returns a string representation of an object.
toTimeString Method Returns a time as a string value.
toUTCString Method Returns a date converted to a string using UTC.
valueOf Method Returns the primitive value of the specified object.

See Also

Calculating Dates and Times (JavaScript)
Date and Time Strings