This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

Date

Summary

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 ]]]])
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 Formatting Date and Time Strings (Windows Scripting - JScript). 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.

Examples

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>

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 Date and Time Calculations (Windows Scripting - JScript) for more information about how to use the Date object and related methods.

Properties

The following table lists the properties of the Date object.

Functions

The following table lists the functions of the Date object.

Methods

The following table lists the methods of the Date object.

MethodSummary
getDateGets the day-of-the-month, using local time.
getDayGets the day of the week, using local time.
getFullYearGets the year, using local time.
getHoursGets the hours in a date, using local time.
getMillisecondsGets the milliseconds of a Date, using local time.
getMinutesGets the minutes of a Date object, using local time.
getMonthGets the month, using local time.
getSecondsGets the seconds of a Date object, using local time.
getTimeGets the time value in milliseconds.
getTimezoneOffsetGets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC).
getUTCDateGets the day-of-the-month, using Universal Coordinated Time (UTC).
getUTCDayGets the day of the week using Universal Coordinated Time (UTC).
getUTCFullYearGets the year using Universal Coordinated Time (UTC).
getUTCHoursGets the hours value in a Date object using Universal Coordinated Time (UTC).
getUTCMillisecondsGets the milliseconds of a Date object using Universal Coordinated Time (UTC).
getUTCMinutesGets the minutes of a Date object using Universal Coordinated Time (UTC).
getUTCMonthGets the month of a Date object using Universal Coordinated Time (UTC).
getUTCSecondsGets the seconds of a Date object using Universal Coordinated Time (UTC).
getYearGets the year of a Date object. Deprecated in favor of getFullYear method.
setDateSets the numeric day-of-the-month value of the Date object using local time.
setFullYearSets the year of the Date object using local time.
setHoursSets the hour value in the Date object using local time.
setMillisecondsSets the milliseconds value in the Date object using local time.
setMinutesSets the minutes value in the Date object using local time.
setMonthSets the month value in the Date object using local time.
setSecondsSets the seconds value in the Date object using local time.
setTimeSets the date and time value in the Date object.
setUTCDateSets the numeric day of the month in the Date object using Universal Coordinated Time (UTC).
setUTCFullYearSets the year value in the Date object using Universal Coordinated Time (UTC).
setUTCHoursSets the hours value in the Date object using Universal Coordinated Time (UTC).
setUTCMillisecondsSets the milliseconds value in the Date object using Universal Coordinated Time (UTC).
setUTCMinutesSets the minutes value in the Date object using Universal Coordinated Time (UTC).
setUTCMonthSets the month value in the Date object using Universal Coordinated Time (UTC).
setUTCSecondsSets the seconds value in the Date object using Universal Coordinated Time (UTC).
setYearSets the year value in the Date object. Deprecated in favor of the setFullYear method.
toDateStringReturns the date component of a date as a human readable string.
toGMTStringReturns a date converted to a string formatted according to GMT conventions.
toISOStringReturns a date as a string value in simplified ISO 8601 Extended format.
toLocaleDateStringReturns a date as a string value appropriate to the host environment’s current locale.
toTimeStringReturns the time component of a date as a human readable string.
toUTCStringReturns a date converted to a string formatted according to UTC conventions.
valueOfReturns the stored time value in milliseconds since midnight, January 1, 1970 UTC.
hasOwnPropertyDetermines whether an object has a property with the specified name.
isPrototypeOfDetermines whether an object exists in another object’s prototype chain.

Properties

The following table lists properties of the Date Object.

PropertyDescription
constructorSpecifies the function that creates an object.
prototypeReturns a reference to the prototype for a class of objects.

Functions

The following table lists functions of the Date Object.

FunctionsDescription
Date.nowReturns the number of milliseconds between January 1, 1970, and the current date and time.
Date.parseParses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.
Date.UTCReturns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the supplied date.

Attributions

  • Microsoft Developer Network: Article