JavaScript Tutorial: Dates
[this page | pdf | back links]
JavaScript
date variables are objects and contain dates and times. They can be
instantiated in 4 ways:
var d1 = new Date(); // An as yet undefined date
var d2 = new Date(milliseconds); // See below
var d3 = new Date(dateString); // See below
var d4 = new Date(year, month, day, hours, minutes,
seconds, milliseconds);
Here milliseconds
refers to the number of milliseconds since 1 January 1970 00:00:00. A dateString
is a textual representation of a date.
The Date object supports
the following properties and methods:
Properties:
Property
|
Description
|
More
|
constructor
|
Returns object’s
constructor function
|
Here
|
prototype
|
Allows author to add
properties and methods to an object
|
Here
|
Methods:
Method
|
Description
|
More
|
getDate()
|
Returns day of month (1 to 31)
|
Here
|
getDay()
|
Returns day of week (0 to 6)
|
Here
|
getFullYear()
|
Returns year
|
Here
|
getHours()
|
Returns hour (0 to 23)
|
Here
|
getMilliseconds()
|
Returns milliseconds (0 to 999)
|
Here
|
getMinutes()
|
Returns minutes (0 to 59)
|
Here
|
getMonth()
|
Returns month (0 to 11)
|
Here
|
getSeconds()
|
Returns seconds (0 to 59)
|
Here
|
getTime()
|
Returns number of milliseconds since 1 January 1970 00:00:00
|
Here
|
getTimezoneOffset()
|
Returns time difference between UTC time and local time, in
minutes
|
Here
|
getUTCDate()
|
Returns UTC day of
month (1 to 31)
|
Here
|
getUTCDay()
|
Returns UTC day of week
(0 to 6)
|
Here
|
getUTCFullYear()
|
Returns UTC year
|
Here
|
getUTCHours()
|
Returns UTC hour (0 to
23)
|
Here
|
getUTCMilliseconds()
|
Returns UTC
milliseconds (0 to 999)
|
Here
|
getUTCMinutes()
|
Rounds UTC minutes (0
to 59)
|
Here
|
getUTCMonth()
|
Returns UTC month (0 to
11)
|
Here
|
getUTCSeconds()
|
Returns UTC seconds (0
to 59)
|
Here
|
getYear()
|
Depreciated.
Use getFullYear() instead
|
Here
|
now()
|
Returns current date and time, as number of milliseconds since 1
January 1970 00:00:00
|
Here
|
parse()
|
Parses a dateString
and returns the number of milliseconds since 1 January 1970 00:00:00
|
Here
|
setDate()
|
Sets day of month
|
Here
|
setFullYear()
|
Sets year (and optionally month
and day)
|
Here
|
setHours()
|
Sets hours (and
optionally minutes, seconds and milliseconds)
|
Here
|
setMilliseconds()
|
Sets miliseconds
|
Here
|
setMinutes()
|
Sets minutes (and
optionally seconds and milliseconds)
|
Here
|
setMonth()
|
Sets month (and
optionally day)
|
Here
|
setSeconds()
|
Sets seconds (and
optionally milliseconds)
|
Here
|
setTime()
|
Sets a date given a specified number of milliseconds since 1 January
1970 00:00:00
|
Here
|
setUTCDate()
|
Sets UTC day of month
|
Here
|
setUTCFullYear()
|
Sets UTC year (and
optionally month and day)
|
Here
|
setUTCHours()
|
Sets UTC hours (and
optionally minutes, seconds and milliseconds)
|
Here
|
setUTCMilliseconds()
|
Sets UTC miliseconds
|
Here
|
setUTCMinutes()
|
Sets UTC minutes (and
optionally seconds and milliseconds)
|
Here
|
setUTCMonth()
|
Sets UTC month (and
optionally day)
|
Here
|
setUTCSeconds()
|
Sets UTC seconds (and
optionally milliseconds)
|
Here
|
setYear()
|
Depreciated. Use
setFullYear() instead
|
|
toDateString()
|
Returns date portion as a string
|
Here
|
toGMTString()
|
Depreciated. Use
toUTCString() instead
|
|
toISOString()
|
Returns date as a string, using ISO notation
|
Here
|
toJSON()
|
Returns date as a
string, using JSON notation
|
Here
|
toLocaleDateString()
|
Returns date portion as a string, using locale-specified
notation
|
Here
|
toLocaleTimeString()
|
Returns time portion as
a string, using locale-specified notation
|
Here
|
toLocaleString()
|
Returns date (and time)
as a string, using locale-specified notation
|
Here
|
toString()
|
Returns date (and time)
as a string
|
Here
|
toUTCString()
|
Returns UTC date (and
time) as a string
|
Here
|
UTC()
|
Returns number of UTC milliseconds
since 1 January 1970 00:00:00
|
Here
|
valueOf()
|
Returns the primitive
value of the object
|
Here
|
NAVIGATION LINKS
Contents | Prev | Next | JavaScript Numbers and Math