-
Book Overview & Buying
-
Table Of Contents
Object-Oriented JavaScript
The Date constructor can be used with several types of input:
You can pass values for year, month, date of the month, hour, minute, second and millisecond, like so:
>>> new Date(2011, 0, 1, 13, 30, 35, 500)
Sat Jan 01 2011 13:30:35 GMT-0800 (Pacific Standard Time)
You can skip any of the input parameters, in which case they are assumed to be 0. Note that month values are from 0 (January) to 11 (December), hours are from 0 to 23, minutes and seconds 0 to 59, and milliseconds 0 to 999.
You can pass a timestamp:
>>> new Date(1293917435500)
Sat Jan 01 2011 13:30:35 GMT-0800 (Pacific Standard Time)
If you don't pass anything, the current date/time is assumed:
>>> new Date()
Fri Apr 18 2008 01:13:00 GMT-0700 (Pacific Daylight Time)
If you pass a string, it's parsed in attempt to extract a possible date value:
>>> new Date('May 4, 2008')Sun May 04 2008 00:00:00 GMT-0700 (Pacific Daylight Time)
|
Property/Method |
Description |
|---|---|
|
|
Similar to passing a string to >>> Date.parse('May 4, 2008')
1209884400000 >>> Date.parse('4th')
NaN |
|
|
Returns a timestamp but in UTC (Coordinated Universal Time), not in local time. >>> Date.UTC(2011, 0, 1, 13, 30, 35, 500) 1293888635500 |
|
Property/Method |
Description/Example |
|---|---|
|
|
Same as >>> var d = new Date(2010, 0, 1); >>> d.toString() "Fri Jan 01 2010 00:00:00 GMT-0800 (Pacific Standard Time)" >>> d.toUTCString() "Fri, 01 Jan 2010 08:00:00 GMT" |
|
|
Returns only the date portion of >>> new Date(2010, 0, 1).toDateString(); "Fri Jan 01 2010" |
|
|
Returns only the time portion of >>> new Date(2010, 0, 1).toTimeString(); "00:00:00 GMT-0800 (Pacific Standard Time)" |
|
|
Equivalent to >>> new Date(2010, 0, 1).toString(); "Fri Jan 01 2010 00:00:00 GMT-0800 (Pacific Standard Time)" >>> new Date(2010, 0, 1).toLocaleString(); "Friday, January 01, 2010 12:00:00 AM" |
|
|
Get or set the time (using a timestamp) of a date object. The following example creates a date and moves it one day forward: >>> var d = new Date(2010, 0, 1); >>> d.getTime(); 1262332800000 >>> d.setTime(d.getTime()+ 1000 * 60 * 60 * 24); 1262419200000 >>> d.toLocaleString() "Saturday, January 02, 2010 12:00:00 AM" |
|
|
Get/Set a full year using local or UTC time. There is also >>> var d = new Date(2010, 0, 1); >>> d.getYear() 110 >>> d.getFullYear() 2010 >>> d.setFullYear(2011) 1293868800000 >>> d Sat Jan 01 2011 00:00:00 GMT-0800 (Pacific Standard Time) |
|
|
Get/Set month, starting from 0 (January): >>> var d = new Date(2010, 0, 1); >>> d.getMonth() 0 >>> d.setMonth(11) 1291190400000 >>> d.toLocaleDateString() "Wednesday, December 01, 2010" |
|
|
Get/Set date of the month. >>> var d = new Date(2010, 0, 1); >>> d.toLocaleDateString() "Friday, January 01, 2010" >>> d.getDate(); 1 >>> d.setDate(31); 1264924800000 >>> d.toLocaleDateString() "Sunday, January 31, 2010" |
|
|
Get/Set hour, minutes, seconds, milliseconds, all starting from 0. >>> var d = new Date(2010, 0, 1); >>> d.getHours() + ':' + d.getMinutes() "0:0" >>> d.setMinutes(59) 1262336399000 >>> d.getHours() + ':' + d.getMinutes() "0:59" |
|
|
Returns the difference between local and universal (UTC) time, measured in minutes. For example the difference between PST (Pacific Standard Time) and UTC: >>> new Date().getTimezoneOffset() 420 >>> 420/60 7 |
|
|
Returns the day of the week, starting from 0 (Sunday): >>> var d = new Date(2010, 0, 1); >>> d.toLocaleDateString() "Friday, January 01, 2010" >>> d.getDay() 5 >>> var d = new Date(2010, 0, 3); >>> d.toLocaleDateString() "Sunday, January 03, 2010" >>> d.getDay() 0 |
Change the font size
Change margin width
Change background colour