“Time flows, JavaScript calculates… sometimes wrong, but with good intentions.”
The first moment you need to work with date and time in JavaScript usually goes like this:
“Let me take this date… add 3 days… print it to the screen…”
And then the Date object whispers from behind:
“By the way, months start from 0 😏”
🧠 1️⃣ What Is the Date Object? (Short but Critical)
Date is JavaScript’s built-in object that manages everything related to time, such as:
- Date
- Time
- Day
- Month
- Year
- Milliseconds
But here’s the key thing you must know:
JavaScript does not store time as a string.
It stores time as milliseconds passed since January 1, 1970.
Yes… according to JavaScript, time begins in 1970.
Before that? Doesn’t exist. Dinosaurs are sad 🦖
🧱 2️⃣ Ways to Create a Date Object (Real-World Usage)
🟢 A) Current Date and Time
const now = new Date();
console.log(now);
📌 What does this do?
- Gets the exact date and time at the moment the code runs.
📌 When is it used?
- “Current time” information
- Logging
- Form submission timestamps
- Counters, countdowns, duration measurements
🟢 B) Creating a Date from a String (THE SAFEST WAY)
const meetingDate = new Date("2026-01-05");
console.log(meetingDate);
🎯 We use the ISO format:
YYYY-MM-DD
📌 Why?
- Works the same in all browsers
- Prevents “It worked in Chrome but Safari crashed” dramas
❌ DO NOT DO THIS:
new Date("05-01-2026"); // Browser war starts
🟢 C) Creating a Date with Numbers (There’s a Trap ⚠️)
const date = new Date(2026, 0, 5);
What does this mean?
- Year: 2026
- Month: 0 (January)
- Day: 5
📌 Months start from 0
- 0 → January
- 1 → February
- 11 → December
JavaScript basically says:
“Don’t come here without memorizing the months.”
🔍 3️⃣ Getting Information from a Date (Getter Methods)
const d = new Date();
📅 Year – Month – Day
d.getFullYear(); // 2026
d.getMonth(); // 0 - 11
d.getDate(); // Day of the month (1-31)
🧠 TIP:
getMonth()is not human-friendly- Add
+1when displaying it on the screen
📆 Day of the Week
d.getDay(); // 0 - 6
- 0 → Sunday
- 1 → Monday
- 6 → Saturday
📌 getDay() ≠ getDate()
| Method | Returns |
|---|---|
| getDay() | Day of the week |
| getDate() | Day of the month |
Anyone who mixes these up eventually becomes a frontend developer 😄
⏰ Hours – Minutes – Seconds
d.getHours();
d.getMinutes();
d.getSeconds();
d.getMilliseconds();
🎯 Use cases:
- Digital clocks
- Live stream durations
- Quiz timers
- “Did this user click within 3 seconds?” analysis
🛠️ 4️⃣ Modifying Dates (Setters)
const d = new Date();
d.setFullYear(2030);
d.setMonth(11); // December
d.setDate(25);
🎄 New date: December 25, 2030
📌 The nice part:
- JavaScript automatically handles overflows
➕➖ 5️⃣ Adding / Subtracting Days, Hours, Months (MOST PRACTICAL PART)
➕ Add 1 Day
const today = new Date();
today.setDate(today.getDate() + 1);
📌 What happened?
- Today’s day was retrieved
- 1 was added
- The Date object updated itself
🧠 What if it’s the end of the month?
- January 31 + 1 → February 1
JavaScript won’t abandon you here 👏
➖ Subtract 7 Days
const d = new Date();
d.setDate(d.getDate() - 7);
🎯 Perfect for “one week ago” calculations.
⏰ Add Hours
const d = new Date();
d.setHours(d.getHours() + 3);
Used for:
- Timezone differences
- Broadcast time calculations
⏳ 6️⃣ The Timestamp Reality (Raw Time)
Date.now();
What does this return?
👉 Milliseconds since 1970
Alternative:
new Date().getTime();
📌 Where is it used?
- Duration measurement
- Performance testing
- Event ordering
- Cache validation
⏱️ 7️⃣ Measuring Duration (Real-Life Example)
const start = Date.now();
// heavy operation 😅
for (let i = 0; i < 1_000_000; i++) {}
const end = Date.now();
console.log(`Operation time: ${end - start} ms`);
🎯 This helps you:
- See if your code is slow or fast
- Answer “Why is this function lagging?”
🌍 8️⃣ Displaying Dates Like a Human (VERY IMPORTANT)
❌ NEVER SHOW THIS:
console.log(new Date());
User reaction:
“What is this, bro?”
✅ Show with Locale
const d = new Date();
d.toLocaleDateString("tr-TR");
// 05.01.2026
d.toLocaleString("en-US");
// 1/5/2026, 10:23:00 AM
📌 Golden rule:
Always display dates to users in locale format.
😬 9️⃣ The Dark Side of the Date Object
Let’s be honest:
❌ Months start from 0
❌ Timezones are confusing
❌ Formatting is painful
❌ Readability is low
That’s why people use:
dayjsdate-fns
BUT 👇
Using a library without understanding Date
is like driving a car without knowing the clutch.
🎯 CONCLUSION: Make Peace with Date
- The Date object is tricky but powerful
- If you know the basics, it gets the job done
- For large projects, libraries make life easier
