Date
Introduction
JavaScript Date represents a moment in time.
- Date reference (developer.mozilla.org)
Constructor
// Now
new Date()
// ms since epoch
new Date(1393678859000)
// Date format
new Date("March 1, 2014 13:00:59")
// ISO date format
new Date("2014-03-01T13:00:59")
new Date(2014, 2, 1, 13, 0, 59, 0)
See: Date() constructor (developer.mozilla.org)
Arguments
| Date | Year | Month | Day | Hour | Min | Sec | Milli |
|---|---|---|---|---|---|---|---|
new Date( | 2014, | 2, | 1, | 13, | 0, | 59, | 0) |
| Date | Year | Month | Day | Hour | Min | Sec | Milli |
Months are zero-indexed (eg, January is 0).
See: Date() constructor (developer.mozilla.org)
Conversion
| Method | Result |
|---|---|
d.toString() | "Sat Mar 01 2014 13:00:59 GMT+0000 (GMT)" |
d.toTimeString() | "13:00:59 GMT+0000 (GMT)" |
d.toUTCString() | "Sat, 01 Mar 2014 13:00:59 GMT" |
d.toDateString() | "Sat Mar 01 2014" |
d.toISOString() | "2014-03-01T13:00:59.000Z" |
d.toLocaleString() | "3/1/2014, 1:00:59 PM" |
d.toLocaleTimeString() | "1:00:59 PM" |
d.getTime() | 1393678859000 |
String results depend on the timezone and locale.
See: toISOString(), toUTCString(), toLocaleString() (developer.mozilla.org)
Accessing
Getters
| Method | Result |
|---|---|
.getDate() | 1..31 |
.getDay() | 0..6 (sun..sat) |
.getFullYear() | 2014 |
.getMonth() | 0..11 |
.getHours() | 0..23 |
.getMinutes() | 0..59 |
.getSeconds() | 0..59 |
.getMilliseconds() | 0..999 |
.getTime() | ms since epoch |
.getTimezoneOffset() | minutes |
See: getTime(), getTimezoneOffset() (developer.mozilla.org)
Setters
| Method | Result |
|---|---|
.setDate (val) | ms since epoch |
.setFullYear (val) | ms since epoch |
.setMonth (val) | ms since epoch |
.setHours (val) | ms since epoch |
.setMinutes (val) | ms since epoch |
.setSeconds (val) | ms since epoch |
.setMilliseconds (val) | ms since epoch |
.setTime (val) | ms since epoch |
UTC versions are also available (eg, .getUTCDate(), .setUTCDate(), etc).
See: setTime() (developer.mozilla.org)
0 Comments for this cheatsheet. Write yours!