JavaScript Dates & Time
Handling dates and times is a critical part of almost every web application. In JavaScript, time manipulation revolves around the Date object. It's a built-in constructor containing highly specific timestamp data along with methods for extracting formatting information.
Instantiation
To capture the current exact moment in time, we invoke the constructor with the new keyword. You can also pass string values or individual time numbers to create historical or future dates.const rightNow = new Date();
const y2k = new Date('2000-01-01');
The 0-Indexed Trap
When using getter methods like getFullYear() or getDate() the return values make perfect sense. However, the getMonth() method acts like an array index! January is 0, and December is 11. Always remember to add 1 when displaying a numerical month to users.
Timestamps & Unix Epoch
At the core of the Date object is the Unix Epoch Timestamp. JavaScript measures time internally in milliseconds elapsed since January 1, 1970. You can get this raw integer using getTime(). This is heavily used for calculating durations (e.g., subtracting an old timestamp from a new one).
View Full Transcript+
This section contains the full detailed transcript covering JS dates. It explores Date instantiation, Date prototype methods like getFullYear, getMonth, setTime, and toISOString(). It also details the historic quirks of JavaScript dates (copied from early Java implementations) which results in 0-indexed months and the deprecated getYear() method which should never be used.
