JavaScript Dates Demystified
Handling dates and times is notoriously tricky in any programming language, but JavaScript's built-in Date object comes with a few specific quirks that every developer must learn to navigate.
Creation & Instantiation
To work with dates, you first need to create an instance of the Date object. Using new Date() without arguments generates an object representing the exact millisecond the code was executed.
You can also parse strings like new Date('2026-03-02'), but parsing can sometimes be inconsistent across different browsers depending on the string format.
The 0-Indexed Month Trap
When you provide numerical arguments to the constructor: new Date(year, month, day), there is a massive gotcha.Months are 0-indexed. January is 0, February is 1... and December is 11. However, days of the month are 1-indexed (starting at 1). Forgetting this rule is the cause of countless off-by-one errors in calendar applications.
Getters and Setters
Once your Date object is ready, you shouldn't modify the raw string it outputs. Instead, use methods like .getFullYear(), .getMonth(), and .getDate() to extract values. You can mathematically alter the date by using setters, such as setDate(myDate.getDate() + 5) to jump five days into the future.
The Unix Epoch Explained+
Underneath it all, a JavaScript Date object is just a number. It represents the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC. This specific point in time is called the Unix Epoch. Using Date.now() returns this number directly without instantiating a full object, which is highly efficient for performance tracking and simple chronometers.
