JAVASCRIPT MASTER CLASS /// LEARN DATES /// UNIX EPOCH /// JAVASCRIPT MASTER CLASS /// LEARN DATES /// UNIX EPOCH /// JAVASCRIPT MASTER CLASS /// LEARN DATES /// UNIX EPOCH ///

JavaScript Dates

Control time itself. Master timestamps, the Date object, formatting tricks, and the infamous 0-indexed month bug.

dates.js
1 / 13
12345
πŸ•°οΈ

Tutor:Working with Dates in JavaScript can be tricky. It all starts with the global 'Date' object. Let's create our first date instance.


Skill Matrix

UNLOCK NODES BY MASTERING JS DATES.

Concept: Instantiation

To begin working with dates, use the new Date() constructor.

System Check

What happens if you run new Date() with absolutely no arguments?


Community Holo-Net

Discuss Time Zones & Epochs

ACTIVE

Struggling with Daylight Savings bugs? Share your woes and learn from other developers.

JavaScript Dates Demystified

Author

Pascual Vila

Frontend Instructor // Code Syllabus

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.

Dates Glossary

new Date()

Instantiates a new Date object. Without arguments, it defaults to the current moment.

snippet.js

Date.now()

Returns the numeric value corresponding to the current timeβ€”the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

snippet.js

getFullYear()

Returns the year of the specified date according to local time (e.g., 2026). Avoid using the deprecated getYear() method.

snippet.js

getMonth()

Returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

snippet.js

getTime()

Returns the number of milliseconds since the ECMAScript epoch. Useful for comparing two dates chronologically.

snippet.js

toLocaleDateString()

Returns a string with a language sensitive representation of the date portion of this date.

snippet.js