49
toLocaleTimeString()
Returns a string that represents the time portion of the date, expressed in the local time
zone, using the local time formatting conventions.
toString()
Converts a Date to a string using the local time zone.
toTimeString()
Returns a string that represents the time portion of the date, expressed in the local time
zone.
toUTCString()
Converts a Date to a string, using universal time.
valueOf()
Converts a Date to its internal millisecond format.
Static Methods
In addition to the many instance methods listed previously, the Date object also defines three
static methods. These methods are invoked through the
Date()
constructor itself, not through
individual Date objects:
Date.now()
Returns the current time, as milliseconds since the epoch.
Date.parse()
Parses a string representation of a date and time and returns the internal millisecond
representation of that date.
Date.UTC()
Returns the millisecond representation of the specified UTC date and time.
Description
The Date object is a datatype built into the JavaScript language. Date objects are created with
the
new Date()
syntax shown earlier.
Once a Date object is created, a number of methods allow you to operate on it. Most methods
simply allow you to get and set the year, month, day, hour, minute, second, and millisecond
fields of the object, using either local time or UTC (universal, or GMT) time. The
toString()
method and its variants convert dates to human-readable strings.
getTime()
and
setTime()
convert to and from the internal representation of the Date object—the number of
milliseconds since midnight (GMT) on January 1, 1970. In this standard millisecond format,
a date and time are represented by a single integer, which makes date arithmetic particularly
easy. The ECMAScript standard requires the Date object to be able to represent any date and
time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a
range of plus or minus 273,785 years, so the JavaScript clock will not “roll over” until the year
275755.
Date
Core JavaScript Reference | 745
Core JavaScript
Reference
How to C#: Basic SDK Concept of XDoc.PDF for .NET You may add PDF document protection functionality into your C# program. Hyperlink Edit. XDoc.PDF for .NET allows C# developers to edit hyperlink of PDF document
pdf link to email; add links to pdf online VB.NET PDF: Basic SDK Concept of XDoc.PDF You may add PDF document protection functionality into your VB.NET program. Hyperlink Edit. XDoc.PDF for .NET allows VB.NET developers to edit hyperlink of PDF
clickable pdf links; accessible links in pdf
48
Examples
Once you create a Date object, there are a variety of methods you can use to operate on it:
d = new Date(); // Get the current date and time
document.write('Today is: " + d.toLocaleDateString() + '. '); // Display date
document.write('The time is: ' + d.toLocaleTimeString()); // Display time
var dayOfWeek = d.getDay(); // What weekday is it?
var weekend = (dayOfWeek == 0) || (dayOfWeek == 6); // Is it a weekend?
Another common use of the Date object is to subtract the millisecond representations of the
current time from some other time to determine the difference between the two times. The
following client-side example shows two such uses:
<script language="JavaScript">
today = new Date(); // Make a note of today's date
christmas = new Date(); // Get a date with the current year
christmas.setMonth(11); // Set the month to December...
christmas.setDate(25); // and the day to the 25th
// If Christmas hasn't already passed, compute the number of
// milliseconds between now and Christmas, convert this
// to a number of days and print a message
if (today.getTime() < christmas.getTime()) {
difference = christmas.getTime() - today.getTime();
difference = Math.floor(difference / (1000 * 60 * 60 * 24));
document.write('Only ' + difference + ' days until Christmas!<p>');
}
</script>
// ... rest of HTML document here ...
<script language="JavaScript">
// Here we use Date objects for timing
// We divide by 1000 to convert milliseconds to seconds
now = new Date();
document.write('<p>It took ' +
(now.getTime()-today.getTime())/1000 +
'seconds to load this page.');
</script>
See Also
Date.parse()
,
Date.UTC()
Date.getDate()
return the day-of-the-month field of a Date
Synopsis
date.getDate()
Returns
The day of the month of the specified Date object
date
, using local time. Return values are
between 1 and 31.
Date.getDate()
746 | Core JavaScript Reference
39
Date.getDay()
return the day-of-the-week field of a Date
Synopsis
date.getDay()
Returns
The day of the week of the specified Date object
date
, using local time. Return values are
between 0 (Sunday) and 6 (Saturday).
Date.getFullYear()
return the year field of a Date
Synopsis
date.getFullYear()
Returns
The year that results when
date
is expressed in local time. The return value is a full four-digit
year, including the century, not a two-digit abbreviation.
Date.getHours()
return the hours field of a Date
Synopsis
date.getHours()
Returns
The hours field, expressed in local time, of the specified Date object
date
. Return values are
between 0 (midnight) and 23 (11 p.m.).
Date.getMilliseconds()
return the milliseconds field of a Date
Synopsis
date.getMilliseconds()
Returns
The milliseconds field, expressed in local time, of
date
.
Date.getMilliseconds()
Core JavaScript Reference | 747
Core JavaScript
Reference
40
Date.getMinutes()
return the minutes field of a Date
Synopsis
date.getMinutes()
Returns
The minutes field, expressed in local time, of the specified Date object
date
. Return values
are between 0 and 59.
Date.getMonth()
return the month field of a Date
Synopsis
date.getMonth()
Returns
The month field, expressed in local time, of the specified Date object
date
. Return values are
between 0 ( January) and 11 (December).
Date.getSeconds()
return the seconds field of a Date
Synopsis
date.getSeconds()
Returns
The seconds field, expressed in local time, of the specified Date object
date
. Return values are
between 0 and 59.
Date.getTime()
return a Date in milliseconds
Synopsis
date.getTime()
Returns
The millisecond representation of the specified Date object
date
—that is, the number of mil-
liseconds between midnight (GMT) on 1/1/1970 and the date and time specified by
date
.
Date.getMinutes()
748 | Core JavaScript Reference
54
Description
getTime()
converts a date and time to a single integer. This is useful when you want to compare
two Date objects or to determine the time elapsed between two dates. Note that the milli-
second representation of a date is independent of the time zone, so there is no
getUTCTime()
method in addition to this one. Don’t confuse this
getTime()
method with the
getDay()
and
getDate()
methods, which return the day of the week and the day of the month, respectively.
Date.parse()
and
Date.UTC()
allow you to convert a date and time specification to a millisec-
ond representation without going through the overhead of first creating a Date object.
See Also
Date
,
Date.parse()
,
Date.setTime()
,
Date.UTC()
Date.getTimezoneOffset()
determine the offset from GMT
Synopsis
date.getTimezoneOffset()
Returns
The difference, in minutes, between GMT and local time.
Description
getTimezoneOffset()
returns the number of minutes difference between the GMT or UTC
time and the local time. In effect, this function tells you what time zone the JavaScript code
is running in and whether or not daylight saving time is (or would be) in effect at the specified
date
.
The return value is measured in minutes, rather than hours, because some countries have time
zones that are not at even one-hour intervals.
Date.getUTCDate()
return the day-of-the-month field of a Date (universal time)
Synopsis
date.getUTCDate()
Returns
The day of the month (a value between 1 and 31) that results when
date
is expressed in
universal time.
Date.getUTCDate()
Core JavaScript Reference | 749
Core JavaScript
Reference
37
Date.getUTCDay()
return the day-of-the-week field of a Date (universal time)
Synopsis
date.getUTCDay()
Returns
The day of the week that results when
date
is expressed in universal time. Return values are
between 0 (Sunday) and 6 (Saturday).
Date.getUTCFullYear()
return the year field of a Date (universal time)
Synopsis
date.getUTCFullYear()
Returns
The year that results when
date
is expressed in universal time. The return value is a full four-
digit year, not a two-digit abbreviation.
Date.getUTCHours()
return the hours field of a Date (universal time)
Synopsis
date.getUTCHours()
Returns
The hours field, expressed in universal time, of
date
. The return value is an integer between
0 (midnight) and 23 (11 p.m.).
Date.getUTCMilliseconds()
return the milliseconds field of a Date (universal time)
Synopsis
date.getUTCMilliseconds()
Returns
The milliseconds field, expressed in universal time, of
date
.
Date.getUTCDay()
750 | Core JavaScript Reference
41
Date.getUTCMinutes()
return the minutes field of a Date (universal time)
Synopsis
date.getUTCMinutes()
Returns
The minutes field, expressed in universal time, of
date
. The return value is an integer between
0 and 59.
Date.getUTCMonth()
return the month-of-the-year field of a Date (universal time)
Synopsis
date.getUTCMonth()
Returns
The month of the year that results when
date
is expressed in universal time. The return value
is an integer between 0 ( January) and 11 (December). Note that the Date object represents
the first day of the month as 1 but represents the first month of the year as 0.
Date.getUTCSeconds()
return the seconds field of a Date (universal time)
Synopsis
date.getUTCSeconds()
Returns
The seconds field, expressed in universal time, of
date
. The return value is an integer between
0 and 59.
Date.getYear()
deprecated
return the year field of a Date
Synopsis
date.getYear()
Returns
The year field of the specified Date object
date
minus 1900.
Date.getYear()
Core JavaScript Reference | 751
Core JavaScript
Reference
43
Description
getYear()
returns the year field of a specified Date object minus 1900. As of ECMAScript v3,
it is not required in conforming JavaScript implementations; use
getFullYear()
instead.
Date.now()
ECMAScript 5
return the current time in milliseconds
Synopsis
Date.now()
Returns
The current time, in milliseconds since midnight GMT on January 1, 1970.
Description
Prior to ECMAScript 5, you can implement this method like this:
Date.now = function() { return (new Date()).getTime(); }
See Also
Date
,
Date.getTime()
Date.parse()
parse a date/time string
Synopsis
Date.parse(date)
Arguments
date
A string containing the date and time to be parsed.
Returns
The number of milliseconds between the specified date and time and midnight GMT on
January 1, 1970.
Description
Date.parse()
is a static method of Date. It parses the date specified by its single string argument
returns it as the number of milliseconds since the epoch. This return value can be used directly,
used to create a new Date object, or used to set the date in an existing Date object with
Date.setTime()
.
ECMAScript 5 requires this method to be able to parse strings returned by the
Date.toISO
String()
method. In ECMAScript 5 and before, this method is also required to be able to
Date.now()
752 | Core JavaScript Reference
Documents you may be interested
Documents you may be interested