Chapter 3. Dates, Time, and Timers
3.0. Introduction
JavaScript’s date and time functionality is quite extensive, and more than sufficient for most applications.
The Date object contains a number representing the date and time, rather
than a string representation. The numeric value for the Date object is the number of seconds since
January 01, 1970 UTC. Leap seconds are ignored.
Strings used to create dates are parsed and converted into this numeric value. Older browsers required that this string be UTC (Coordinated Time Universal or Greenwich Mean Time) format. Beginning with ECMAScript 5, ISO 8601 parsing is supported, which I’ll cover later in the chapter.
The Date Object
Dates are managed through the Date object. You can create a date using a
variety of techniques (see Recipe 3.1 for a discussion of
different approaches), and modify the date using an extensive number of
methods.
You can also access every aspect of a date: year, month, day of
week, time, and so on, using specialized get and set
methods, described in Tables 3-1 and 3-2.
Method | Purpose |
| Returns day of the month (0–31) |
| Returns day of the week (0–6) |
| Returns 4-digit full year |
| Returns local hour (0–23) |
| Returns local milliseconds (0–999) |
| Returns local minute (0–59) |
| Returns local month (0–11) |
| Returns local second (0–59) |
| Returns number of seconds since January 1, 1970 00:00:00 UTC |
| Returns time zone from UTC ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access