Working with Dates and Times
Reading the clock, doing date math, and dodging the classic traps
Date object. It is powerful, but it has a few famous surprises. Once you know them, dates stop being scary.
- make a
Datefor now or for a specific day, - read the year, month, day, and time out of a date,
- compare two dates and do arithmetic (like "how many days until?"),
- understand timestamps, durations, and the zero-based-month, time-zone, and daylight-saving traps.
1. Making a Date
A Date is an object that holds one exact moment in time. You create one with new Date(...). There are three ways beginners use most:
const now = new Date(); // right now, this very moment
const party = new Date(2026, 0, 15); // a specific day: 15 January 2026
const iso = new Date('2026-07-15'); // from a text string (ISO format)
Watch out - months start at 0! In new Date(2026, 0, 15) the middle number is the month, and January is 0, not 1. So December is 11. The day-of-month, oddly, still starts at 1. This "zero-based month" is the single most famous Date trap - keep it in mind the whole way through.
0, the last (December) is at slot 11. The day number, though, is written on the page like a normal calendar: the 15th is 15.
Calling new Date() with no arguments is the one you will use most: it captures the current date and time.