The following table displays a summary of the date methods. Detailed descriptions of each method follow the table.
Returns the day of the month for the specified date.
Date
getDate()
The value returned by getDate is an integer between 1 and 31.
The second statement below assigns the value 25 to the variable day, based on the value of the Date object Xmas95.
Date: setDate
Returns the day of the week for the specified date.
Date
getDay()
The value returned by getDay is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
The second statement below assigns the value 1 to weekday, based on the value of the Date object Xmas95. December 25, 1995, is a Monday.
Returns the hour for the specified date.
Date
getHours()
The value returned by getHours is an integer between 0 and 23.
The second statement below assigns the value 23 to the variable hours, based on the value of the Date object Xmas95.
Date: setHours
Returns the minutes in the specified date.
Date
getMinutes()
The value returned by getMinutes is an integer between 0 and 59.
The second statement below assigns the value 15 to the variable minutes, based on the value of the Date object Xmas95.
Date: setMinutes
Returns the month in the specified date.
Date
getMonth()
The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January 1 to February, and so on.
The second statement below assigns the value 11 to the variable month, based on the value of the Date object Xmas95.
Date: setMonth
Returns the seconds in the current time.
Date
getSeconds()
The value returned by getSeconds is an integer between 0 and 59.
The second statement below assigns the value 30 to the variable secs, based on the value of the Date object Xmas95.
Date: setSeconds
Returns the numeric value corresponding to the time for the specified date.
Date
getTime()
The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00. You can use this method to help assign a date and time to another Date object.
The following example assigns the date value of theBigDay to sameAsBigDay:
theBigDay = new Date("July 1, 1999") sameAsBigDay = new Date() sameAsBigDay.setTime(theBigDay.getTime())
Date: setTime
Returns the time-zone offset in minutes for the current locale.
Date
getTimezoneOffset()
The time-zone offset is the difference between local time and Greenwich Mean Time (GMT). Daylight savings time prevents this value from being a constant.
Returns the year in the specified date.
Date
getFullYear()
The value returned by getFullYear is the four-digit year. For example, if the year is 1856, the value returned is 1856. If the year is 2026, the value returned is 2026.
The second statement assigns the value 1995 to the variable year.
The second statement assigns the value 2000 to the variable year.
The second statement assigns the value 95 to the variable year, representing the year 1995.
Date: setYear
Returns the number of milliseconds in a date string since January 1, 1970, 00:00:00, local time. The parse method is static, read only.
Date
Date.parse(dateString)
dateString
The parse method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 (local time). This function is useful for setting date values based on string values, for example in conjunction with the setTime method and the Date object.
Given a string representing a time, parse returns the time value. It accepts the IETF standard date syntax: "Mon, 25 Dec 1995 13:30:00 GMT." It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.
Because parse is a static method of Date, you always use it as Date.parse(), rather than as a method of a Date object you created.
If IPOdate is an existing Date object, then you can set it to August 9, 1995 as follows:
IPOdate.setTime(Date.parse("Aug 9, 1995"))
Sets the day of the month for a specified date.
Date
setDate(dayValue)
datValue
An integer from 1 to 31, representing the day of the month.
The second statement below changes the day for theBigDay to July 24 from its original value.
Date: getDate
Sets the hours for a specified date.
Date
setHours(hoursValue)
hoursValue
An integer between 0 and 23, representing the hour.
theBigDay.setHours(7)
Sets the minutes for a specified date.
Date
setMinutes(minutesValue)
mintuesValue
An integer between 0 and 59, representing the minutes.
theBigDay.setMinutes(45)
Date: getMinutes
Sets the month for a specified date.
Date
setMonth(monthValue)
monthValue
An integer between 0 and 11, representing the months January through December.
theBigDay.setMonth(6)
Date: getMonth
Sets the seconds for a specified date.
Date
setSeconds(secondsValue)
secondsValue
theBigDay.setSeconds(30)
Date: getSeconds
Sets the value of a Date object.
Date
setTime(timevalue)
timevalue
An integer representing the number of milliseconds since 1 January 1970 00:00:00.
Use the setTime method to help assign a date and time to another Date object.
Sets the year for a specified date.
Date
setYear(yearValue)
yearValue
If yearValue is a number between 0 and 99 (inclusive), then the year for dateObjectName is set to 1900 + yearValue. Otherwise, the year for dateObjectName is set to yearValue.
Note that there are two ways to set years in the 20th century.
Date: getFullYear
Converts a date to a string, using the Internet GMT conventions.
Date
toGMTString()
The exact format of the value returned by toGMTString varies according to the platform.
In the following example, today is a Date object:
today.toGMTString()
In this example, the toGMTString method converts the date to GMT (UTC) using the operating system's time-zone offset and returns a string value that is similar to the following form. The exact format depends on the platform.
Mon, 18 Dec 1995 17:28:35 GMT
Date: toLocaleString
Converts a date to a string, using the current locale's conventions.
Date
toLocaleString()
If you pass a date using toLocaleString, be aware that different platforms assemble the string in different ways. Using methods such as getHours, getMinutes, and getSeconds gives more portable results.
In the following example, today is a Date object:
In this example, toLocaleString returns a string value that is similar to the following form. The exact format depends on the platform.
12/18/95 17:28:35
Date: toGMTString
Returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, Universal Coordinated Time (GMT). UTC is static, read only.
Date
Date.UTC(year, month, day, hrs, min, sec)
year
month
date
A day of the month between 1 and 31.
hrs
(Optional) A number of hours between 0 and 23.
min
(Optional) A number of minutes between 0 and 59.
sec
(Optional) A number of seconds between 0 and 59.
UTC takes comma-delimited date parameters and returns the number of milliseconds since January 1, 1970, 00:00:00, Universal Coordinated Time (GMT).
Because UTC is a static method of Date, you always use it as Date.UTC(), rather than as a method of a Date object you created.
The following statement creates a Date object using GMT instead of local time:
gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0))