Table of Contents | Previous
| Next
| Index
Lets you work with dates and times.
The Date
constructor:
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(yr_num, mo_num, day_num[, hr_num, min_num, sec_num])
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. Dates prior to 1970 are not allowed.
JavaScript depends on platform-specific date facilities and behavior; the behavior of the Date
object varies from platform to platform.
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
, getFullUTCYear
, and setFullUTCYear
.
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;
In addition, this object inherits the watch
and unwatch
methods from Object
.
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)
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 Object.constructor
.
Returns the day of the month for the specified date according to local time.
getDate()
None
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
.
Xmas95 = new Date("December 25, 1995 23:15:00")
day = Xmas95.getDate()
Date.setDate
Returns the day of the week for the specified date according to local time.
getDay()
None
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.
Xmas95 = new Date("December 25, 1995 23:15:00")
weekday = Xmas95.getDay()
Date.setDate
Returns the hour for the specified date according to local time.
getHours()
None
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
.
Xmas95 = new Date("December 25, 1995 23:15:00")
hours = Xmas95.getHours()
Date.setHours
Returns the minutes in the specified date according to local time.
getMinutes()
None
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
.
Xmas95 = new Date("December 25, 1995 23:15:00")
minutes = Xmas95.getMinutes()
Date.setMinutes
Returns the month in the specified date according to local time.
getMonth()
None
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
.
Xmas95 = new Date("December 25, 1995 23:15:00")
month = Xmas95.getMonth()
Date.setMonth
Returns the seconds in the current time according to local time.
getSeconds()
None
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
.
Xmas95 = new Date("December 25, 1995 23:15:30")
secs = Xmas95.getSeconds()
Date.setSeconds
Returns the numeric value corresponding to the time for the specified date according to local time.
getTime()
None
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.
getTimezoneOffset()
None
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.
x = new Date()
currentTimeZoneOffsetInHours = x.getTimezoneOffset()/60
Returns the year in the specified date according to local time.
getYear()
None
The getYear
method returns either a 2-digit or 4-digit year:
Example 1. The second statement assigns the value 95 to the variable year
.
Xmas = new Date("December 25, 1995 23:15:00")
year = Xmas.getYear() // returns 95
Example 2. The second statement assigns the value 100 to the variable year
.
Xmas = new Date("December 25, 2000 23:15:00")
year = Xmas.getYear() // returns 100
Example 3. The second statement assigns the value -100 to the variable year
.
Xmas = new Date("December 25, 1800 23:15:00")
year = Xmas.getYear() // returns -100
Example 4. The second statement assigns the value 95 to the variable year
, representing the year 1995.
Xmas.setYear(95)
year = Xmas.getYear() // returns 95
Date.setYear
Returns the number of milliseconds in a date string since January 1, 1970, 00:00:00, local time.
Date.parse(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"))
Date.UTC
Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. For information on prototypes, see Function.prototype
.
Sets the day of the month for a specified date according to local time.
setDate(dayValue)
The second statement below changes the day for theBigDay
to July 24 from its original value.
theBigDay = new Date("July 27, 1962 23:30:00")
theBigDay.setDate(24)
Date.getDate
Sets the hours for a specified date according to local time.
setHours(hoursValue)
theBigDay.setHours(7)
Date.getHours
Sets the minutes for a specified date according to local time.
setMinutes(minutesValue)
theBigDay.setMinutes(45)
Date.getMinutes
Sets the month for a specified date according to local time.
setMonth(monthValue)
theBigDay.setMonth(6)
Date.getMonth
Sets the seconds for a specified date according to local time.
setSeconds(secondsValue)
theBigDay.setSeconds(30)
Date.getSeconds
Sets the value of a Date
object according to local time.
setTime(timevalue)
Use the setTime
method to help assign a date and time to another Date
object.
theBigDay = new Date("July 1, 1999")
sameAsBigDay = new Date()
sameAsBigDay.setTime(theBigDay.getTime())
Date.getTime
Sets the year for a specified date according to local time.
setYear(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.
Example 1. The year is set to 1996.
theBigDay.setYear(96)
Example 2. The year is set to 1996.
theBigDay.setYear(1996)
Example 3. The year is set to 2000.
theBigDay.setYear(2000)
Date.getYear
Converts a date to a string, using the Internet GMT conventions.
toGMTString()
None
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.
toLocaleString()
None
If you pass a date using toLocaleString
, be aware that different platforms assemble the string in different ways. Methods such as getHours
, getMinutes
, and getSeconds
give more portable results.
The toLocaleString
method 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, toLocaleString
returns a string that is not year-2000 compliant. toLocaleString
behaves similarly to toString
when converting a year that the operating system does not properly format.
In the following example, today
is a Date
object:
today = new Date(95,11,18,17,28,35) //months are represented by 0 to 11
today.toLocaleString()
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 a string representing the specified Date object.
toString()
None.
The Date
object overrides the toString
method of the Object
object; it does not inherit Object.toString
. For Date
objects, the toString
method returns a string representation of the object.
JavaScript calls the toString
method automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.
The following example assigns the toString
value of a Date object to myVar
:
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
Object.toString
Returns the number of milliseconds in a Date
object since January 1, 1970, 00:00:00, universal time.
Date.UTC(year, month, day[, hrs, min, sec])
UTC
takes 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 UTC
method differs from the Date
constructor in two ways.
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))
Date.parse
Returns the primitive value of a Date object.
valueOf()
None
The valueOf
method of Date
returns the primitive value of a Date object as a number data type, the number of milliseconds since midnight 01 January, 1970 UTC.
This method is usually called internally by JavaScript and not explicitly in code.
x = new Date(56,6,17);
myVar=x.valueOf() //assigns -424713600000 to myVar
Object.valueOf
Table of Contents | Previous
| Next
| Index
Last Updated: 11/13/98 10:23:00
Copyright � 1998
Netscape Communications Corporation