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 ...
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.