JavaScript — Dates
Creating dates
new Date(); // right now
new Date("2026-08-23"); // from ISO string
new Date(2026, 7, 23); // year, MONTH?, day — see trap below
new Date(1790000000000); // milliseconds since epoch (Jan 1 1970)
Date.now(); // current ms timestamp — cheap & comparable
The month trap
Months are zero-indexed:
new Date(2026, 0, 1); // Jan 1 (0 = January!)
new Date(2026, 11, 31); // Dec 31 (11 = December!)
Days are 1-based; months are 0-based. Everyone gets bitten once.
Getting parts
const d = new Date();
d.getFullYear(); // 2026
d.getMonth(); // 7 → August (0-based!)
d.getDate(); // 23 day of month
d.getDay(); // 0=Sun…6=Sat
d.getHours(); // 19
d.getMinutes();
d.getSeconds();
d.getTime(); // ms timestamp
Formatting
Built-in locale strings
d.toLocaleDateString(); // "23/08/2026" (per user locale)
d.toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric" });
// "Sunday, August 23"
d.toLocaleTimeString("en-GB"); // "19:45:12"
d.toISOString(); // "2026-08-23T19:45:12.999Z" — machine standard
toISOString is what you send to servers/databases; toLocale* is what humans read.
Manual template
const pad = n => String(n).padStart(2, "0");
`${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`; // 2026-08-23
Math with dates
Subtract two Dates → milliseconds between them:
const start = new Date("2026-01-01");
const end = new Date("2026-08-23");
const days = Math.round((end - start) / 86400000); // ~234
Constants: 1000 ms/s · 86400000 ms/day.
Add days safely by mutation:
const later = new Date(start);
later.setDate(later.getDate() + 30); // auto-rolls across months/years ✓
Parsing pitfalls
new Date("08/23/2026") // US-format dependent — avoid
new Date("2026-08-23") // ISO date-only → parsed as UTC midnight ⚠️
new Date("2026-08-23T10:00") // local time
For anything serious, store ISO strings or timestamps; parse with intent.
Modern alternative
Intl.DateTimeFormat and the newer Temporal proposal handle calendars/timezones rigorously; libraries (date-fns/dayjs) fill production gaps. For tutorials, native Date suffices.
Mini Practice
- Print today's weekday name via toLocaleDateString options
- Days-until-New-Year calculator using timestamps
- Build
formatYYYYMMDD(d)with padStart - Add one month to Jan 31 — observe rollover behavior; explain it
- Convert an ISO string to a readable local string both ways
Next: math → (already covered in numbers)
Related Topics
Frequently Asked Questions about Dates
What is Dates in JavaScript?
Dates is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Dates?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Dates.
Why is Dates important in JavaScript?
Dates is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.