Date : Object
Browser/User Agent Support
| IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Constructors
| Constructor | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
|
Creates a new instance of a Date object.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
Properties
| Property | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
|
Specifies the function that creates an object's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
|
Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Methods
| Method | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
|
Returns the day of the month for the specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns a number representing the day of the week for the specified date (according to local time).
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the year of the specified date according to local time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the hour for the specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the number of milliseconds in the specified date according to local time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the minutes in the specified date according to local time.
| 4.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the month (from 0-11) in the specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the seconds in the specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the numeric value corresponding to the time for the specified date according to universal time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the time-zone offset in minutes for the current locale.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the day (date) of the month in the specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the day of the week in the specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the year in the specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the hours in the specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the milliseconds in the specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the minutes in the specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the month of the specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the seconds in the specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Returns the year in the specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the number of milliseconds passed since 1970-01-01 00:00:00 UTC.
| no | 1.0+ | no | no | no |
|
Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00, local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Sets the day of the month for a specified date according to local time.
| 4.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Sets the full year for a specified date according to local time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the hours for a specified date according to local time.
| 4.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Sets the milliseconds for a specified date according to local time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the minutes for a specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Set the month for a specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Sets the seconds for a specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Sets the value of a Date object according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Sets the day of the month for a specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the full year for a specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the hour for a specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the milliseconds for a specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the minutes for a specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the month for a specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the seconds for a specified date according to universal time.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Sets the year for a specified date according to local time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns a string version of the Date object expressed in local time.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
|
Converts a date to a string, using Internet GMT conventions.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
|
Converts a date to a string, returning the "date" portion using the current locale's conventions.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Converts a date to a string, using the current locale's conventions.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Converts a date to a string, returning the "date" portion using the current locale's conventions.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns a string representing the source code of a Date object.
| 3.0+ | 1.0+ | 2.0+ | no | no |
|
Returns a string representing the specified Date object.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
|
Returns a string representation of the time portion of a Date object, expressed in local time.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
|
Converts a date to a string, using the universal time convention.
| 4.0+ | 1.0+ | 4.06+ | 7.0+ | 1.0+ |
|
Accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time.
| 3.0+ | 1.0+ | 2.0+ | 7.0+ | 1.0+ |
|
Returns the primitive value of a Date object.
| 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Several ways to assign dates
The following examples show several ways to assign dates:
today = new Date();
birthday = new Date("December 17, 1995 03:24:00");
birthday = new Date(95,11,17);
birthday = new Date(95,11,17,3,24,0);
Remarks
If you supply no arguments, the constructor creates a 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 date is measured in milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.
The Date object provides uniform behavior across platforms.
The 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.
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.
The following example returns the time elapsed between timeA and timeB in milliseconds.
timeA = new Date(); // Statements here to take some action. timeB = new Date(); timeDifference = timeB - timeA;
References
Availability
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
Constructor Detail
Date Date([Number milliseconds,] [String dateString,] [Object yr_num, mo_num, day_num,] [Object hr_num, min_num, sec_num, ms_num])
Creates a new instance of a Date object.
| Number | milliseconds | The elements to add to the front of the array. (optional) |
| String | dateString | String value representing a date. The string should be in a format recognized by the parse method. (optional) |
| Object | yr_num, mo_num, day_num | Integer values representing part of a date. As an integer value, the month is represented by 0 to 11 with 0=January and 11=December. (optional) |
| Object | hr_num, min_num, sec_num, ms_num | Integer values representing part of a date. (optional) |
Property Detail
Function constructor
Specifies the function that creates an object's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name.
-
See Also
- Availability
-
JavaScript 1.1 | NES 2.0 | ECMA-262
Date prototype
Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class.
-
See Also
- Availability
-
JavaScript 1.1 | NES 2.0 | ECMA-262
Method Detail
getDate() : Number
Returns the day of the month for the specified date according to local time.
-
Using
getDateThe second statement below assigns the value 25 to the variable
day, based on the value of theDateobjectXmas95.Xmas95 = new Date("December 25, 1995 23:15:00") day = Xmas95.getDate()
Remarks
-
The value returned by
getDateis an integer between 1 and 31.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
getDay() : Number
Returns a number representing the day of the week for the specified date (according to local time).
-
Using
getDayThe second statement below assigns the value 1 to
weekday, based on the value of theDateobjectXmas95. December 25, 1995, is a Monday.Xmas95 = new Date("December 25, 1995 23:15:00") weekday = Xmas95.getDay();
Remarks
-
The value returned by
getDayis an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
getFullYear() : Number
Returns the year of the specified date according to local time.
-
Using
getFullYearThe following example assigns the four-digit value of the current year to the variable
yr.var yr; Today = new Date(); yr = Today.getFullYear();
Remarks
-
The value returned by
getFullYearis an absolute number. For dates between the years 1000 and 9999,getFullYearreturns a four-digit number, for example, 1995. Use this function to make sure a year is compliant with years after 2000.Use this method instead of the
getYearmethod.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getHours() : Number
Returns the hour for the specified date according to local time.
-
Using
getHoursThe second statement below assigns the value 23 to the variable
hours, based on the value of theDateobjectXmas95.Xmas95 = new Date("December 25, 1995 23:15:00") hours = Xmas95.getHours()
Remarks
-
The value returned by
getHoursis an integer between 0 and 23.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
getMilliseconds() : Number
Returns the number of milliseconds in the specified date according to local time.
-
Using
getMillisecondsThe following example assigns the milliseconds portion of the current time to the variable
ms.var ms; Today = new Date(); ms = Today.getMilliseconds();
Remarks
-
The value returned by
getMillisecondsis a number between 0 and 999.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getMinutes() : Number
Returns the minutes in the specified date according to local time.
-
Using
getMinutesThe second statement below assigns the value 15 to the variable
minutes, based on the value of theDateobjectXmas95.Xmas95 = new Date("December 25, 1995 23:15:00") minutes = Xmas95.getMinutes()
Remarks
-
The value returned by
getMinutesis an integer between 0 and 59.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
getMonth() : Number
Returns the month (from 0-11) in the specified date according to local time.
-
Using
getMonthThe second statement below assigns the value 11 to the variable
month, based on the value of theDateobjectXmas95.Xmas95 = new Date("December 25, 1995 23:15:00") month = Xmas95.getMonth()
Remarks
-
The value returned by
getMonthis an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
getSeconds() : Number
Returns the seconds in the specified date according to local time.
-
Using
getSecondsThe second statement below assigns the value 30 to the variable
secs, based on the value of theDateobjectXmas95.Xmas95 = new Date("December 25, 1995 23:15:30") secs = Xmas95.getSeconds()
Remarks
-
The value returned by
getSecondsis an integer between 0 and 59.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
getTime() : Number
Returns the numeric value corresponding to the time for the specified date according to universal time.
-
Using
getTimeThe following example assigns the date value of
theBigDaytosameAsBigDay:theBigDay = new Date("July 1, 1999") sameAsBigDay = new Date() sameAsBigDay.setTime(theBigDay.getTime())
Remarks
-
The value returned by the
getTimemethod 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 anotherDateobject.This method is functionally equivalent to the valueOf method.
See Also
-
parse | UTC | getUTCHours | setTime | valueOf
Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
getTimezoneOffset() : Number
Returns the time-zone offset in minutes for the current locale.
-
Using
getTimezoneOffsetx = new Date() currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60
Remarks
- 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. Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
getUTCDate() : Number
Returns the day (date) of the month in the specified date according to universal time.
-
Using
getUTCDateThe following example assigns the day portion of the current date to the variable
d.var d; Today = new Date(); d = Today.getUTCDate();
Remarks
-
The value returned by
getUTCDateis an integer between 1 and 31.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getUTCDay() : Number
Returns the day of the week in the specified date according to universal time.
-
Using
getUTCDayThe following example assigns the weekday portion of the current date to the variable
weekday.var weekday; Today = new Date() weekday = Today.getUTCDay()
Remarks
-
The value returned by
getUTCDayis an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getUTCFullYear() : Number
Returns the year in the specified date according to universal time.
-
Using
getUTCFullYearThe following example assigns the four-digit value of the current year to the variable
yr.var yr; Today = new Date(); yr = Today.getUTCFullYear();
Remarks
-
The value returned by
getUTCFullYearis an absolute number that is compliant with year-2000, for example, 1995.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getUTCHours() : Number
Returns the hours in the specified date according to universal time.
-
Using
getUTCHoursThe following example assigns the hours portion of the current time to the variable
hrs.var hrs; Today = new Date(); hrs = Today.getUTCHours();
Remarks
-
The value returned by
getUTCHoursis an integer between 0 and 23.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getUTCMilliseconds() : Number
Returns the milliseconds in the specified date according to universal time.
-
Using
getUTCMilliseconds=The following example assigns the milliseconds portion of the current time to the variable
ms.var ms; Today = new Date(); ms = Today.getUTCMilliseconds();
Remarks
-
The value returned by
getUTCMillisecondsis an integer between 0 and 999.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getUTCMinutes() : Number
Returns the minutes in the specified date according to universal time.
-
Using
getUTCMinutesThe following example assigns the minutes portion of the current time to the variable
min.var min; Today = new Date(); min = Today.getUTCMinutes();
Remarks
-
The value returned by
getUTCMinutesis an integer between 0 and 59.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getUTCMonth() : Number
Returns the month of the specified date according to universal time.
-
Using
getUTCMonthThe following example assigns the month portion of the current date to the variable
mon.var mon; Today = new Date(); mon = Today.getUTCMonth();
Remarks
-
The value returned by
getUTCMonthis an integer between 0 and 11 corresponding to the month. 0 for January, 1 for February, 2 for March, and so on.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getUTCSeconds() : Number
Returns the seconds in the specified date according to universal time.
-
Using
getUTCSecondsThe following example assigns the seconds portion of the current time to the variable
sec.var sec; Today = new Date(); sec = Today.getUTCSeconds();
Remarks
-
The value returned by
getUTCSecondsis an integer between 0 and 59.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
getYear() : Number
Returns the year in the specified date according to local time.
-
Years between 1900 and 1999
The second statement assigns the value 95 to the variable
year.Xmas = new Date("December 25, 1995 23:15:00") year = Xmas.getYear() // returns 95Years above 1999
The second statement assigns the value 100 to the variable
year.Xmas = new Date("December 25, 2000 23:15:00") year = Xmas.getYear() // returns 100Years below 1900
The second statement assigns the value -100 to the variable
year.Xmas = new Date("December 25, 1800 23:15:00") year = Xmas.getYear() // returns -100Setting and getting a year between 1900 and 1999
The second statement assigns the value 95 to the variable
year, representing the year 1995.Xmas.setYear(95) year = Xmas.getYear() // returns 95
Remarks
-
getYearis no longer used and has been replaced by the getFullYear method.The
getYearmethod returns the year minus 1900; thus:- For years above 2000, the value returned by
getYearis 100 or greater. For example, if the year is 2026,getYearreturns 126.
- For years between and including 1900 and 1999, the value returned by
getYearis between 0 and 99. For example, if the year is 1976,getYearreturns 76.
- For years less than 1900 or greater than 1999, the value returned by
getYearis less than 0. For example, if the year is 1800,getYearreturns -100.
To take into account years before and after 2000, you should use
getFullYearinstead ofgetYearso that the year is specified in full.
See Also
- For years above 2000, the value returned by
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1 | deprecated by ECMAScript v3
now() : Number
Returns the number of milliseconds passed since 1970-01-01 00:00:00 UTC.
-
Remarks
- SpiderMonkey extension to ECMAScript standard. Supported by Mozilla only. Availability
-
parse(String dateString) : static Number
Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00, local time.
| String | dateString | A string representing a date. |
-
Using
parseIf
IPOdateis an existingDateobject, then you can set it to August 9, 1995 as follows:IPOdate.setTime(Date.parse("Aug 9, 1995")) ;
Remarks
-
The
parsemethod 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 thesetTimemethod and theDateobject.Given a string representing a time,
parsereturns 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
parseis a static method ofDate, you always use it asDate.parse(), rather than as a method of aDateobject you created.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
setDate(Number dayValue) : Number
Sets the day of the month for a specified date according to local time.
| Number | dayValue | An integer from 1 to 31, representing the day of the month. |
-
Using
setDateThe second statement below changes the day for
theBigDayto July 24 from its original value.theBigDay = new Date("July 27, 1962 23:30:00") theBigDay.setDate(24)
Remarks
- Before this method was standardized to ECMAScript, it returned nothing. See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
setFullYear(Number yearValue, [Number monthValue,] [Number dayValue]) : Number
Sets the full year for a specified date according to local time.
| Number | yearValue | An integer specifying the numeric value of the year, for example, 1995. |
| Number | monthValue | An integer between 0 and 11 representing the months January through December. (optional) |
| Number | dayValue | An integer between 1 and 31 representing the day of the month. If you specify the dayValue parameter, you must also specify the monthValue. (optional) |
-
Using
setFullYeartheBigDay = new Date(); theBigDay.setFullYear(1997);
Remarks
-
If you do not specify the
monthValueanddayValueparameters, the values returned from thegetMonthandgetDatemethods are used.If a parameter you specify is outside of the expected range,
setFullYearattempts to update the other parameters and the date information in theDateobject accordingly. For example, if you specify 15 formonthValue, the year is incremented by 1 (year + 1), and 3 is used for the month.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setHours(Number hoursValue, [Number minutesValue,] [Number secondsValue,] [Number msValue]) : Number
Sets the hours for a specified date according to local time.
| Number | hoursValue | An integer between 0 and 23, representing the hour. |
| Number | minutesValue | An integer between 0 and 59, representing the minutes. (optional) |
| Number | secondsValue | An integer between 0 and 59, representing the seconds. If you specify the secondsValue parameter, you must also specify the minutesValue. (optional) |
| Number | msValue | A number between 0 and 999, representing the milliseconds. If you specify the msValue parameter, you must also specify the minutesValue and secondsValue. (optional) |
-
Using
setHourstheBigDay.setHours(7)
Remarks
-
Before this method was standardized to ECMAScript, it returned nothing. If you do not specify the
minutesValue,secondsValue, andmsValueparameters, the values returned from thegetUTCMinutes,getUTCSeconds, andgetMillisecondsmethods are used.If a parameter you specify is outside of the expected range,
setHoursattempts to update the date information in theDateobject accordingly. For example, if you use 100 forsecondsValue, the minutes will be incremented by 1 (min + 1), and 40 will be used for seconds.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
setMilliseconds(Number millisecondsValue) : Number
Sets the milliseconds for a specified date according to local time.
| Number | millisecondsValue | A number between 0 and 999, representing the milliseconds. |
-
Using
setMillisecondstheBigDay = new Date(); theBigDay.setMilliseconds(100);
Remarks
-
If you specify a number outside the expected range, the date information in the
Dateobject is updated accordingly. For example, if you specify 1005, the number of seconds is incremented by 1, and 5 is used for the milliseconds.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setMinutes(Number minutesValue, [Number secondsValue,] [Number msValue]) : Number
Sets the minutes for a specified date according to local time.
| Number | minutesValue | An integer between 0 and 59, representing the minutes. |
| Number | secondsValue | An integer between 0 and 59, representing the seconds. If you specify the secondsValue parameter, you must also specify the minutesValue. (optional) |
| Number | msValue | A number between 0 and 999, representing the milliseconds. If you specify the msValue parameter, you must also specify the minutesValue and secondsValue. (optional) |
-
Using
setMinutestheBigDay.setMinutes(45)
Remarks
-
Before this method was standardized to ECMAScript, it returned nothing. If you do not specify the
secondsValueandmsValueparameters, the values returned fromgetSecondsandgetMillisecondsmethods are used.If a parameter you specify is outside of the expected range,
setMinutesattempts to update the date information in theDateobject accordingly. For example, if you use 100 forsecondsValue, the minutes (minutesValue) will be incremented by 1 (minutesValue+ 1), and 40 will be used for seconds.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
setMonth(Number monthValue, [Number dayValue]) : Number
Set the month for a specified date according to local time.
| Number | monthValue | An integer between 0 and 11 (representing the months January through December). |
| Number | dayValue | An integer from 1 to 31, representing the day of the month. (optional) |
-
Using
setMonththeBigDay.setMonth(6)
Remarks
-
Before this method was standardized to ECMAScript, it returned nothing. If you do not specify the
dayValueparameter, the value returned from thegetDatemethod is used.If a parameter you specify is outside of the expected range,
setMonthattempts to update the date information in theDateobject accordingly. For example, if you use 15 formonthValue, the year will be incremented by 1 (year + 1), and 3 will be used for month.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
setSeconds(Number secondsValue, [Number msValue]) : Number
Sets the seconds for a specified date according to local time.
| Number | secondsValue | An integer between 0 and 59. |
| Number | msValue | A number between 0 and 999, representing the milliseconds. (optional) |
-
Using
setSecondstheBigDay.setSeconds(30)
Remarks
-
Before this method was standardized to ECMAScript, it returned nothing. If you do not specify the
msValueparameter, the value returned from thegetMillisecondsmethod is used.If a parameter you specify is outside of the expected range,
setSecondsattempts to update the date information in theDateobject accordingly. For example, if you use 100 forsecondsValue, the minutes stored in theDateobject will be incremented by 1, and 40 will be used for seconds.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
setTime(Number timeValue) : Number
Sets the value of a Date object according to local time.
| Number | timeValue | An integer representing the number of milliseconds since 1 January 1970, 00:00:00. |
-
Using
setTimetheBigDay = new Date("July 1, 1999") sameAsBigDay = new Date(); sameAsBigDay.setTime(theBigDay.getTime());
Remarks
-
Before this method was standardized to ECMAScript, it returned nothing. Use the
setTimemethod to help assign a date and time to anotherDateobject.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
setUTCDate(Number dayValue) : Number
Sets the day of the month for a specified date according to universal time.
| Number | dayValue | An integer from 1 to 31, representing the day of the month. |
-
Using
setUTCDatetheBigDay = new Date(); theBigDay.setUTCDate(20);
Remarks
-
If a parameter you specify is outside of the expected range,
setUTCDateattempts to update the date information in theDateobject accordingly. For example, if you use 40 fordayValue, and the month stored in theDateobject is June, the day will be changed to 10 and the month will be incremented to July.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setUTCFullYear(Number yearValue, [Number monthValue,] [Number dayValue]) : Number
Sets the full year for a specified date according to universal time.
| Number | yearValue | An integer specifying the numeric value of the year, for example, 1995. |
| Number | monthValue | An integer between 0 and 11 representing the months January through December. (optional) |
| Number | dayValue | An integer between 1 and 31 representing the day of the month. If you specify the dayValue parameter, you must also specify the monthValue. (optional) |
-
Using
setUTCFullYeartheBigDay = new Date(); theBigDay.setUTCFullYear(1997);
Remarks
-
If you do not specify the
monthValueanddayValueparameters, the values returned from thegetMonthandgetDatemethods are used.If a parameter you specify is outside of the expected range,
setUTCFullYearattempts to update the other parameters and the date information in theDateobject accordingly. For example, if you specify 15 formonthValue, the year is incremented by 1 (year + 1), and 3 is used for the month.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setUTCHours(Number hoursValue, [Number minutesValue,] [Number secondsValue,] [Number msValue]) : Number
Sets the hour for a specified date according to universal time.
| Number | hoursValue | An integer between 0 and 23, representing the hour. |
| Number | minutesValue | An integer between 0 and 59, representing the minutes. (optional) |
| Number | secondsValue | An integer between 0 and 59, representing the seconds. If you specify the secondsValue parameter, you must also specify the minutesValue. (optional) |
| Number | msValue | A number between 0 and 999, representing the milliseconds. If you specify the msValue parameter, you must also specify the minutesValue and secondsValue. (optional) |
-
Using
setUTCHourstheBigDay = new Date(); theBigDay.setUTCHours(8);
Remarks
-
If you do not specify the
minutesValue,secondsValue, andmsValueparameters, the values returned from thegetUTCMinutes,getUTCSeconds, andgetUTCMillisecondsmethods are used.If a parameter you specify is outside of the expected range,
setUTCHoursattempts to update the date information in theDateobject accordingly. For example, if you use 100 forsecondsValue, the minutes will be incremented by 1 (min + 1), and 40 will be used for seconds.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setUTCMilliseconds(Number millisecondsValue) : Number
Sets the milliseconds for a specified date according to universal time.
| Number | millisecondsValue | A number between 0 and 999, representing the milliseconds. |
-
Using
setUTCMillisecondstheBigDay = new Date(); theBigDay.setUTCMilliseconds(500);
Remarks
-
If a parameter you specify is outside of the expected range,
setUTCMillisecondsattempts to update the date information in theDateobject accordingly. For example, if you use 1100 formillisecondsValue, the seconds stored in theDateobject will be incremented by 1, and 100 will be used for milliseconds.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setUTCMinutes(Number minutesValue, [Number secondsValue,] [Number msValue]) : Number
Sets the minutes for a specified date according to universal time.
| Number | minutesValue | An integer between 0 and 59, representing the minutes. |
| Number | secondsValue | An integer between 0 and 59, representing the seconds. If you specify the secondsValue parameter, you must also specify the minutesValue. (optional) |
| Number | msValue | A number between 0 and 999, representing the milliseconds. If you specify the msValue parameter, you must also specify the minutesValue and secondsValue. (optional) |
-
Using
setUTCMinutestheBigDay = new Date(); theBigDay.setUTCMinutes(43);
Remarks
-
If you do not specify the
secondsValueandmsValueparameters, the values returned fromgetUTCSecondsandgetUTCMillisecondsmethods are used.If a parameter you specify is outside of the expected range,
setUTCMinutesattempts to update the date information in theDateobject accordingly. For example, if you use 100 forsecondsValue, the minutes (minutesValue) will be incremented by 1 (minutesValue+ 1), and 40 will be used for seconds.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setUTCMonth(Number monthValue, [Number dayValue]) : Number
Sets the month for a specified date according to universal time.
| Number | monthValue | An integer between 0 and 11, representing the months January through December. |
| Number | dayValue | An integer from 1 to 31, representing the day of the month. (optional) |
-
Using
setUTCMonththeBigDay = new Date(); theBigDay.setUTCMonth(11);
Remarks
-
If you do not specify the
dayValueparameter, the value returned from thegetUTCDatemethod is used.If a parameter you specify is outside of the expected range,
setUTCMonthattempts to update the date information in theDateobject accordingly. For example, if you use 15 formonthValue, the year will be incremented by 1 (year + 1), and 3 will be used for month.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setUTCSeconds(Number secondsValue, [Number msValue]) : Number
Sets the seconds for a specified date according to universal time.
| Number | secondsValue | An integer between 0 and 59. |
| Number | msValue | A number between 0 and 999, representing the milliseconds. (optional) |
-
Using
setUTCSecondstheBigDay = new Date(); theBigDay.setUTCSeconds(20);
Remarks
-
If you do not specify the
msValueparameter, the value returned from thegetUTCMillisecondsmethods is used.If a parameter you specify is outside of the expected range,
setUTCSecondsattempts to update the date information in theDateobject accordingly. For example, if you use 100 forsecondsValue, the minutes stored in theDateobject will be incremented by 1, and 40 will be used for seconds.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
setYear(Number yearValue) : Number
Sets the year for a specified date according to local time.
| Number | yearValue | An integer. |
-
Using
setYearThe first two lines set the year to 1996. The third sets the year to 2000.
theBigDay.setYear(96) theBigDay.setYear(1996) theBigDay.setYear(2000)
Remarks
-
Before this method was standardized to ECMAScript, it returned nothing.
setYearis no longer used and has been replaced by thesetFullYearmethod.If
yearValue</code. is a number between 0 and 99 (inclusive), then the year for <code>dateObjectNameis set to 1900 +yearValue. Otherwise, the year fordateObjectNameis set toyearValue.To take into account years before and after 2000, you should use
setFullYearinstead ofsetYearso that the year is specified in full.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1 | deprecated by ECMAScript v3
toDateString() : String
Returns a string version of the Date object expressed in local time.
-
See Also
-
Date.toLocaleDateString | Date.toLocaleString | Date.toLocaleTimeString | Date.toString | Date.toTimeString
Availability
-
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
toGMTString() : String
Converts a date to a string, using Internet GMT conventions.
-
Using
toGMTStringIn the following example, today is a
Dateobject:today.toGMTString();
In this example, the
toGMTStringmethod 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
Remarks
-
toGMTStringis no longer used and has been replaced by thetoUTCStringmethod.The exact format of the value returned by
toGMTStringvaries according to the platform.You should use Date.toUTCString instead of
toGMTSTring.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1 | deprecated by ECMAScript v3
toLocaleDateString() : String
Converts a date to a string, returning the "date" portion using the current locale's conventions.
-
Using
toLocaleDateStringIn the following example,
todayis aDateobject:today = new Date(95,11,18,17,28,35) //months are represented by 0 to 11 today.toLocaleDateString()
In this example,
toLocaleDateStringreturns a string value that is similar to the following form. The exact format depends on the platform.12/18/95
Remarks
-
The
toLocaleDateStringmethod relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/98), whereas in Germany the date appears before the month (15.04.98). If the operating system is not year-2000 compliant and does not use the full year for years before 1900 or over 2000,toLocaleDateStringreturns a string that is not year-2000 compliant.toLocaleDateStringbehaves similarly totoStringwhen converting a year that the operating system does not properly format.Methods such as
getHours,getMinutes, andgetSecondsgive more portable results thantoLocaleDateString.
See Also
-
Date.toDateString | Date.toLocaleString | Date.toLocaleTimeString | Date.toString | Date.toTimeString | toGMTString | toUTCString
Availability
-
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
toLocaleString() : String
Converts a date to a string, using the current locale's conventions.
-
Using
toLocaleStringIn the following example,
todayis aDateobject:today = new Date(95,11,18,17,28,35); //months are represented by 0 to 11 today.toLocaleString();
In this example,
toLocaleStringreturns a string value that is similar to the following form. The exact format depends on the platform.12/18/95 17:28:35
Remarks
-
The
toLocaleStringmethod relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/98), whereas in Germany the date appears before the month (15.04.98). If the operating system is not year-2000 compliant and does not use the full year for years before 1900 or over 2000,toLocaleStringreturns a string that is not year-2000 compliant.toLocaleStringbehaves similarly totoStringwhen converting a year that the operating system does not properly format.Methods such as
getHours,getMinutes, andgetSecondsgive more portable results thantoLocaleString.
See Also
-
Date.toLocaleDateString | Date.toLocaleTimeString | Date.toString | Date.toUTCString
Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
toLocaleTimeString() : String
Converts a date to a string, returning the "date" portion using the current locale's conventions.
-
Using
toLocaleTimeStringIn the following example,
todayis aDateobject:today = new Date(95,11,18,17,28,35) //months are represented by 0 to 11 today.toLocaleTimeString()
In this example,
toLocaleTimeStringreturns a string value that is similar to the following form. The exact format depends on the platform.17:28:35
Remarks
-
The
toLocaleTimeStringmethod relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/98), whereas in Germany the date appears before the month (15.04.98). If the operating system is not year-2000 compliant and does not use the full year for years before 1900 or over 2000,toLocaleTimeStringreturns a string that is not year-2000 compliant.toLocaleTimeStringbehaves similarly totoStringwhen converting a year that the operating system does not properly format.Methods such as
getHours,getMinutes, andgetSecondsgive more portable results thantoLocaleTimeString.
See Also
-
Date.toDateString | Date.toLocaleDateString | Date.toLocaleString | Date.toString | Date.toTimeString | toGMTString | toUTCString
Availability
-
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
toSource() : String
Returns a string representing the source code of a Date object.
-
Remarks
-
The
toSourcemethod returns the following values:- For the built-in
Dateobject,toSourcereturns the following string indicating that the source code is not available:
function Date() { [native code] }- For instances of
Date,toSourcereturns a string representing the source code.
This method is usually called internally by JavaScript and not explicitly in code.
See Also
- For the built-in
- Availability
-
JavaScript 1.3 | ECMAScript v1
toString() : String
Returns a string representing the specified Date object.
-
Using
toStringThe following assigns the
toStringvalue of aDateobject tomyVar:x = new Date(); myVar=x.toString(); //assigns a value to myVar similar to: //Mon Sep 28 14:36:22 GMT-0700 (Pacific Daylight Time) 1998
Remarks
-
The
Dateobject overrides thetoStringmethod of theObjectobject; it does not inheritObject.toString. ForDateobjects, thetoStringmethod returns a string representation of the object.JavaScript calls the
toStringmethod automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.
See Also
-
Date.parse | Date.toDateString | Date.toLocaleString | Date.toTimeString | Date.toUTCString
Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1 Overrides Object.toString
toTimeString() : String
Returns a string representation of the time portion of a Date object, expressed in local time.
-
See Also
-
Date.toString | Date.toDateString | Date.toLocaleDateString | Date.toLocaleString | Date.toLocaleTimeString
Availability
-
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
toUTCString() : String
Converts a date to a string, using the universal time convention.
-
Using
toUTCStringvar UTCstring; Today = new Date(); UTCstring = Today.toUTCString();
Remarks
-
The value returned by
toUTCStringis a readable string formatted according to UTC convention. The format of the return value may vary according to the platform.
See Also
- Availability
-
JavaScript 1.2 | JScript 3.0 | ECMAScript v1
UTC(Object year, Number month, [Number date,] [Number hrs,] [Number min,] [Number sec,] [Number ms]) : static Number
Accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time.
| Object | year | A year after 1900. |
| Number | month | An integer between 0 and 11 representing the month. |
| Number | date | An integer between 1 and 31 representing the day of the month. (optional) |
| Number | hrs | An integer between 0 and 23 representing the hours. (optional) |
| Number | min | An integer between 0 and 59 representing the minutes. (optional) |
| Number | sec | An integer between 0 and 59 representing the seconds. (optional) |
| Number | ms | An integer between 0 and 999 representing the milliseconds. (optional) |
-
Using
Date.UTCThe following statement creates a
Dateobject using GMT instead of local time:gmtDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0));
Remarks
-
UTCtakes comma-delimited date parameters and returns the number of milliseconds between January 1, 1970, 00:00:00, universal time and the time you specified.You should specify a full year for the year; for example, 1998. If a year between 0 and 99 is specified, the method converts the year to a year in the 20th century (1900 + year); for example, if you specify 95, the year 1995 is used.
The
UTCmethod differs from theDateconstructor in two ways.Date.UTCuses universal time instead of the local time.
Date.UTCreturns a time value as a number instead of creating aDateobject.
If a parameter you specify is outside of the expected range, the
UTCmethod updates the other parameters to allow for your number. For example, if you use 15 for month, the year will be incremented by 1 (year + 1), and 3 will be used for the month.Because
UTCis a static method ofDate, you always use it asDate.UTC(), rather than as a method of aDateobject you created.
See Also
- Availability
-
JavaScript 1.0 | JScript 1.0 | ECMAScript v1
valueOf() : Number
Returns the primitive value of a Date object.
-
Using
valueOfx = new Date(56, 6, 17); myVar = x.valueOf(); //assigns -424713600000 to myVar
Remarks
-
The
valueOfmethod returns the primitive value of aDateobject as a number data type, the number of milliseconds since midnight 01 January, 1970 UTC.This method is functionally equivalent to the getTime method.
This method is usually called internally by JavaScript and not explicitly in code.
See Also
- Availability
-
JavaScript 1.1 | ECMAScript v1
本文详细介绍了JavaScript中的Date对象,包括构造函数、属性、本地时间和通用时间的方法等,并提供了丰富的使用示例。
1万+

被折叠的 条评论
为什么被折叠?



