const birthday = new Date(2000, 5, 15);
// June 15, 2000… right?console.log(birthday);// 😱 "May 15, 2000" — WHAT?!JavaScript Dates: Creating, Formatting, Comparing, and Manipulating Time
Bottom-Line Answer: JavaScript (JS) is a high-level, single-threaded, just-in-time (JIT) compiled programming language with first-class functions that conforms to the ECMAScript specification. It powers dynamic client-side web interactivity alongside HTML and CSS, and executes backend server runtimes via Node.js, Deno, and Bun.
What is JavaScript Dates? Core Concept & Direct Summary
How do you work with dates and timestamps in JavaScript?
Welcome to JavaScript'sDateobject — powerful, built-in, andfull of quirksthat have been tripping developers since 1995. Months start at 0. Days start at 1. And formatting dates is somehow harder than it should be.
The good news: once you know the handful of rules in this lesson, dates become genuinely easy. You'll learn to create dates, read and change their parts, format them beautifully for any country, compare them, and calculate the difference between two dates. Let's travel through time. ⏳
How Does JavaScript Dates Work? In-Depth Mechanics & Architecture
Creating Dates: The Four Ways 🏗️
① Right Now — No Arguments
② From a Date String
⚠️ Stick to ISO format (
YYYY-MM-DD):strings like"11/08/2026"parse differently across browsers (is that November 8 or August 11?!). ISO format removes all ambiguity.
③ From Individual Components
④ From a Timestamp (Milliseconds)
JavaScript's secret: every date is stored asmilliseconds since January 1, 1970(the "Unix epoch"):
🚨 The Month Trap (Read This Twice!)
Back to our opening bug:
Months are zero-indexed— January is0, December is11:
Why?A design decision copied from Java in 1995 that can never be fixed without breaking the old web — just liketypeof nullfromLesson 5. Days, years, and hours are normal (1-based);only months are weird. Memorize it once, never get bitten again. 🧠
Reading Date Parts: The Getter Family 📖
⚠️ The Two "Day" Traps
Mixing these up is the second-most-common date bug.getDate= calendar day;getDay= weekday.
Changing Dates: The Setter Family ✏️
Every getter has a matching setter:
🎁 Bonus Magic: Automatic Overflow Handling
Setters handle rollovers intelligently — exceed a limit and it carries over:
setDate(getDate() + n)is the standard idiom for "n days from now"— no manual month-length math needed.
Formatting Dates for Humans 🌍
RawDateoutput (Thu Sep 19 2026 14:32:07 GMT+0530) is for machines. For users, usetoLocale*methods — they format forany language and region automatically:
🎨 Full Control with Options
The options object unlocks beautiful custom formats:
One built-in method, every locale on Earth — no formatting library needed for most projects.
Other Built-In Formats (Quick Reference)
💡 Remember Lesson 25:
JSON.stringifyconverts Dates usingtoISOString()— that's why dates come back from APIs as ISO strings. Revive them withnew Date(savedString).
Comparing Dates ⚖️
Dates can't be compared with===(they're objects — reference comparison from Lesson 5!). But<,>,<=,>=work, because JavaScript converts dates to their millisecond timestamps:
Checking Exact Equality — Compare Timestamps
Rule:===asks "same object?";getTime() ===asks "same moment?". You almost always want the second.
Calculating the Difference Between Dates ⏱️
Subtract two dates → getmilliseconds→ convert to whatever unit you need:
🎂 Real Example: Days Until Your Birthday
The conversion ladder to memorize:
🛠️ Mini Project: Age Calculator 🎂
The subtle bug this avoids:naively subtracting years calls someone 26 even before their birthday this year. Real-world date logic is full of these edge cases — and now you know how to catch them. 🏆
Common Beginner Mistakes (And Fixes)
Quick Self-Check: Lesson 33 Quiz 🧠
- What date does
new Date(2026, 0, 15)create — and why not what you expected? - What's the difference between
getDate()andgetDay()? - Why does
new Date("2026-09-19") === new Date("2026-09-19")returnfalse? - Write one line: format a date as "Saturday, 19 September 2026".
- How do you get the number of days between two dates?
(Answers: 1. January 15, 2026 — month 0 is January, since months are zero-indexed · 2. getDate() = day of month (1–31); getDay() = day of week (0=Sunday to 6=Saturday) · 3. They're two different objects in memory — compare moments with getTime() === getTime() · 4.d.toLocaleDateString("en-IN", { weekday: "long", day: "numeric", month: "long", year: "numeric" })· 5. Subtract them (gives milliseconds), then divide by (1000 × 60 × 60 × 24) — round with Math.ceil or Math.floor as needed)
JavaScript Decision Matrix & Syntax Comparison
Understanding the mechanical differences between variable declarations and execution environments is critical for writing performant, bug-free modern JavaScript.
var vs let vs const| Feature / Property | var (Legacy ES5) | let (Modern ES6+) | const (Modern ES6+) |
|---|---|---|---|
| Scope | Function Scope | Block Scope ({}) | Block Scope ({}) |
| Hoisting Behavior | Hoisted (initialized as undefined) | Hoisted (Temporal Dead Zone - ReferenceError) | Hoisted (Temporal Dead Zone - ReferenceError) |
| Reassignable | ✓ Yes | ✓ Yes | ✗ No (Immutable binding) |
| Redeclarable in Same Scope | ✓ Yes (Error-prone) | ✗ No (SyntaxError) | ✗ No (SyntaxError) |
| Window Object Binding | Creates property on window | Does not attach to window | Does not attach to window |
| Dimension | Client-Side (Web Browser) | Server-Side (Node.js / Deno) |
|---|---|---|
| Global Object | window, document, navigator | global, process, globalThis |
| DOM & UI Manipulation | ✓ Full access (document.querySelector) | ✗ No DOM (Headless / Non-UI) |
| File System & OS Access | ✗ Blocked (Sandboxed security) | ✓ Full access (fs, os, child_process) |
| Primary Focus | Interactive UI, animations, client state | REST APIs, microservices, databases, I/O streaming |
Runnable JavaScript Code Example & Practical Implementation
let now = new Date();
console.log(now.toISOString());
let specificDate = new Date('2026-09-19');
console.log(specificDate.getFullYear());
// 2026
console.log(specificDate.getMonth());
// 8 (September)Frequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked questions for frontend & full-stack rolesQ: How do you calculate the difference in days between two Date objects?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online JavaScript Editor & IDE — Browser-based JavaScript workspace with live console and DOM inspector.
- C Programming Tutorial — Master memory allocation, pointers, and systems programming fundamentals.
- C++ Programming Tutorial — Explore Object-Oriented Programming, templates, and high-performance STL.
- Online Code Editors & Compilers — Complete development tool suite for 20+ programming languages.
Authoritative Standards & Citations
- ECMA-262 ECMAScript Language Specification — The official international JavaScript language specification maintained by TC39.
- MDN Web Docs (Mozilla Developer Network) — Authoritative documentation and API guide for JavaScript and Web APIs.
- Google V8 JavaScript Engine — Open-source high-performance JavaScript and WebAssembly engine documentation.
- Node.js Official Documentation — Server-side JavaScript runtime API specification and reference.