Every app deals with dates sooner or later — showing today's date, counting days until a deadline, or formatting a timestamp for a user. JavaScript's Date object handles all of it, but it has a few quirks that surprise every beginner. Learn them once, and dates stop being scary.
Create a date with new Date(). With no arguments you get right now. With arguments you can build a specific date — but remember, months are zero-indexed, so January is 0 and December is 11.
const now = new Date();
console.log(now); // current date and time
const birthday = new Date(2004, 4, 15); // 15 May 2004, not 15 June!
console.log(birthday.getFullYear()); // 2004
console.log(birthday.getMonth()); // 4 (May)
console.log(birthday.getDate()); // 15
The getter methods give you each part of a date, and the setters let you change one part without touching the rest. Adding days is as simple as setting the date to its current value plus a number — JavaScript rolls over into the next month automatically.
const today = new Date();
today.setDate(today.getDate() + 7); // one week from now
const diffMs = today - now; // dates subtract to milliseconds
console.log(Math.round(diffMs / (1000 * 60 * 60 * 24))); // 7
Formatting is where most beginners struggle. toLocaleDateString() is the easiest way to get a human-readable date, and you can pass options for weekday, month style and year. Avoid building date strings by hand — different browsers parse them differently, and that is a classic source of bugs.
const date = new Date(2026, 11, 6);
console.log(date.toLocaleDateString("en-IN", {
weekday: "long",
day: "numeric",
month: "long",
year: "numeric"
})); // "Sunday, 6 December 2026"
Two habits will save you trouble: store dates as timestamps or ISO strings (date.toISOString()) when saving or sending them, and do your date math with the built-in methods instead of string tricks. Dates are fiddly in every language — JavaScript just makes the quirks visible. Learn the month numbering and the ISO format, and you are past the worst of it.
Top comments (0)