Chapter 15. Date and Time
Most real-world applications involve working with date and time data. Unfortunately, JavaScript’s Date
object (which also stores time data) is not one of the language’s best-designed features. Because of the limited utility of this built-in object, I will be introducing Moment.js, which extends the functionality of the Date
object to cover commonly needed functionality.
It’s an interesting bit of history that JavaScript’s Date
object was originally implemented by Netscape programmer Ken Smith—who essentially ported Java’s java.util.Date
implementation into JavaScript. So it’s not entirely true that JavaScript has nothing to do with Java: if anyone ever asks you what they have to do with each other, you can say, “Well, aside from the Date
object and a common syntactic ancestor, very little.”
Because it gets tedious to keep repeating “date and time,” I will use “date” to implicitly mean “date and time.” A date without a time is implicitly 12:00 A.M. on that day.
Dates, Time Zones, Timestamps, and the Unix Epoch
Let’s face it: our modern Gregorian calendar is a fussy, overcomplicated thing, with 1-based numbering, odd divisions of time, and leap years. Time zones add even more complexity. However, it’s (mostly) universal, and we have to live with it.
We’ll start with something simple: the second. Unlike the complicated division of time in the Gregorian calendar, seconds are easy. Dates and times—as represented by seconds—are a single number, neatly ordered ...
Get Learning JavaScript, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.