JavaScript/Dates
From Wikibooks, the open-content textbooks collection
A Date is an object that contains a given time to millisecond precision.
Contents |
[edit] Basic use
Unlike strings and numbers, the date must be explicitly created with the new operator.
var d = new Date();
The Date object may also be created using paramters within its Constructor. By default, the Date object will contain the current date and time found on the computer, but can be set to any date or time desired.
var y2k_crash = new Date(1999, 12, 31, 23,59,59,999); // One more millisecond, and the world ends!
The date object normally stores the value within the local time zone. If UTC is needed, there are a set of functions available for that use.
The Date object does not support non-CE epochs, but can still represent almost any available time within it's available range.
[edit] Properties and methods of the Date() object
As with all objects, the Date() object has methods and properties to manipulate its represented time.
[edit] setFullYear(year), getFullYear()
Stores or retrieves the full 4-digit year within the Date object.
[edit] setMonth(month, day)
Sets the month within the Date object, and optionally the day within the month.
Note: The Date object uses 0 as January instead of 1.
[edit] getMonth()
Returns the current month.
[edit] getDate()
Returns the day of the month.
[edit] getDay()
Returns the day of the week within the object. Note: Sunday is 0, with the other days of the week taking the next value.
[edit] parse(text)
Reads the string text, and returns the number of milliseconds since January 1, 1970.