TalentJR

🟨 JavaScript tutorial for beginners · Class 9 · Lesson 12 of 18 · Free

Working with Dates – The Date Object in JavaScript

⏱ About 40 minutes · ▶ run real JavaScript here — nothing to install · no sign-in

In this lesson you will

  • Create dates with new Date() and read the day, month and year from them
  • Format dates the Indian way (DD/MM/YYYY) and count the days between two dates
  • Add days to a date and build a live countdown on a web page

How many days are left until your board exam? When is the library book due if you borrow it today? What day of the week is Republic Day this year? Apps answer these questions all the time, and in JavaScript they all start with one built-in object: Date.

How do I create a date in JavaScript?

new Date() with nothing inside gives the date and time RIGHT NOW, from the computer's clock.

new Date(year, month, day) makes a fixed date. Careful: months start from 0! January is 0, February is 1, … December is 11. Days start from 1 as usual.

You can add hours and minutes too: new Date(2026, 7, 15, 9, 30) is 15 August 2026, 9:30 in the morning. toDateString() shows a date in a short, readable form.

🟨 JavaScript — you can edit this code
Console output
Today is Thu Dec 03 2026
Mon Jan 26 2026
Sat Aug 15 2026
Hour: 9 Minute: 30

💡 The first line shows today's date, so its output changes every day. Try it: make a date for your own birthday.

How do I get the day, month and year from a date in JavaScript?

A Date has "get" methods: getFullYear(), getMonth() (0–11), getDate() (day of the month, 1–31) and getDay() (day of the week, 0 = Sunday … 6 = Saturday).

getMonth() and getDay() give numbers. To show names, keep arrays of names and use the number as the index.

Note the trap: getDate() is the day of the MONTH, getDay() is the day of the WEEK.

🎬 How JavaScript thinks — step 1 of 10JavaScript is about to run line 1
▶const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
2const months = ["January", "February", "March", "April", "May", "June",
3 "July", "August", "September", "October", "November", "December"];
4
5const diwali = new Date(2026, 10, 8);
6const year = diwali.getFullYear();
7const month = diwali.getMonth();
8const date = diwali.getDate();
9const weekday = diwali.getDay();
10
11console.log(year, month, date, weekday);
12console.log(`Diwali: ${days[weekday]}, ${date} ${months[month]} ${year}`);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
2026 10 8 0
Diwali: Sunday, 8 November 2026

💡 Try it: change the date to your next birthday. Which day of the week is it?

How do I format a date as DD/MM/YYYY in JavaScript?

In India we write dates as day/month/year. Build it yourself: take the parts, add 1 to the month, and use padStart(2, "0") so 5 becomes "05".

The browser can also format for you with toLocaleDateString(). Always pass the locale, like "en-IN", so the result is the same on every computer.

Options choose what to show: weekday, day, month ("long", "short" or "2-digit") and year. "hi-IN" gives Hindi month names.

🟨 JavaScript — you can edit this code
Console output
05/02/2027
Friday, 5 February 2027
5 फ़रवरी

💡 Without "en-IN", toLocaleDateString() follows the computer's own settings, so a laptop in the USA would show 2/5/2027.

How do I find the number of days between two dates?

Inside, a Date is a number: milliseconds counted from 1 January 1970. So when you subtract two dates, you get the gap in milliseconds.

One day = 24 × 60 × 60 × 1000 = 86,400,000 ms. Divide by that to get days.

Use Math.round() at the end. In countries with summer time, one day can be an hour short or long, and rounding fixes that.

🎬 How JavaScript thinks — step 1 of 9JavaScript is about to run line 1
▶const ONE_DAY = 24 * 60 * 60 * 1000;
2const today = new Date(2026, 11, 3);
3const boardExam = new Date(2027, 1, 15);
4
5const gapMs = boardExam - today;
6const daysLeft = Math.round(gapMs / ONE_DAY);
7const weeksLeft = Math.floor(daysLeft / 7);
8
9console.log("Milliseconds:", gapMs);
10console.log(`${daysLeft} days left (${weeksLeft} full weeks)`);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Milliseconds: 6393600000
74 days left (10 full weeks)

💡 Try it: change today to new Date() to count from the real today. Then the output changes each day.

How do I add days to a date in JavaScript?

Use setDate(getDate() + n). JavaScript fixes the month and year for you: 25 December + 14 days becomes 8 January next year.

setDate() CHANGES the date object. Make a copy first with new Date(oldDate) if you still need the original.

A date picker (<input type="date">) gives text like "2026-12-25". Split it into numbers and build the Date yourself. Pick another date on the page and watch the due date change.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it
Console output
Issued Fri Dec 25 2026 → return by Fri Jan 08 2027

💡 m - 1 because the picker counts months from 1, but new Date() counts from 0.

How do I make a live countdown timer in JavaScript?

Combine Date with setInterval from Class 8 Lesson 14. Every second, find target − now and split the milliseconds into days, hours, minutes and seconds.

First turn ms into whole seconds s. One day has 86,400 seconds and one hour 3,600. Math.floor() keeps whole units, and % (remainder) keeps what is left over: hours % 24, minutes % 60, seconds % 60.

This box keeps running on purpose. When the target time is reached, clearInterval stops it.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it

💡 Try it: change the target to your birthday or the last day of exams.

🤔 Predict the output

Think like JavaScript: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

const d = new Date(2026, 0, 31);
d.setDate(d.getDate() + 1);
console.log(d.getMonth(), d.getDate());

Question 2. What does this print?

const christmas = new Date(2026, 11, 25);
console.log(christmas.getMonth());

Question 3. What does this print?

const a = new Date(2026, 0, 1);
const b = new Date(2026, 0, 11);
console.log(b - a);

🧩 Your challenge

Make an "upcoming events" list. Today is fixed as 3 December 2026. For each event, print its date as DD/MM/YYYY, the weekday name, and the days left. Then print the nearest event. Expected output: Christmas: 25/12/2026 (Friday) – 22 days left Republic Day: 26/01/2027 (Tuesday) – 54 days left Holi: 22/03/2027 (Monday) – 109 days left Next up: Christmas

Expected output in the console:
Christmas: 25/12/2026 (Friday) – 22 days left
Republic Day: 26/01/2027 (Tuesday) – 54 days left
Holi: 22/03/2027 (Monday) – 109 days left
Next up: Christmas
🟨 JavaScript — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

const independenceDay = new Date(2026, 8, 15);
console.log(independenceDay.toDateString());

No error, but it prints Tue Sep 15 2026: month 8 is September, not August.

✅ Right

const independenceDay = new Date(2026, 7, 15);
console.log(independenceDay.toDateString());

Months in new Date(year, month, day) start at 0, so August is 7, not 8. Subtract 1 from the month you speak.

❌ Wrong

const examDate = "2027-02-15";
console.log(examDate.getMonth());

TypeError: examDate.getMonth is not a function

✅ Right

const [y, m, d] = "2027-02-15".split("-").map(Number);
const examDate = new Date(y, m - 1, d);
console.log(examDate.getMonth());

A date written as text is just a string, and strings have no Date methods. Turn it into a Date first.

❌ Wrong

const a = new Date(2026, 0, 26);
const b = new Date(2026, 0, 26);
console.log(a === b ? "Same day" : "Different days");

No error, but it prints "Different days": a and b are two separate Date objects, so === is false even though the dates match.

✅ Right

const a = new Date(2026, 0, 26);
const b = new Date(2026, 0, 26);
console.log(a.getTime() === b.getTime() ? "Same day" : "Different days");

=== on two objects checks if they are the SAME object, not if they hold the same date. Compare the numbers from getTime() instead.

📌 Remember

  • new Date() is right now; new Date(2026, 0, 26) is a fixed date — months start at 0.
  • getFullYear(), getMonth(), getDate() and getDay() read parts of a date; use arrays for month and day names.
  • Build DD/MM/YYYY with padStart, or use toLocaleDateString("en-IN", options).
  • Subtracting dates gives milliseconds; divide by 86,400,000 and Math.round() to get days.
  • setDate(getDate() + n) adds days and fixes the month and year for you — copy the date first.
  • Compare dates with getTime(), not ===.

Finished the quiz and the challenge?

Questions students ask

Why does getMonth() return 0 for January in JavaScript?

JavaScript copied this from older languages, where months are positions in a list starting at 0. Days of the month start at 1 as normal. Just remember: add 1 when you show a month, subtract 1 when you create a date.

How do I get today's date in DD/MM/YYYY format?

Use new Date() for today, then build the text with getDate(), getMonth() + 1 and getFullYear(), padding with padStart(2, "0"). Or use new Date().toLocaleDateString("en-IN", { day: "2-digit", month: "2-digit", year: "numeric" }).

Why is my date one day off when I use new Date("2026-01-26")?

A date-only text like "2026-01-26" is read as midnight in UTC (London time), not your local time. In India that is still 26 January, but in the USA it becomes 25 January. Splitting the text and using new Date(year, month - 1, day) avoids this.

Can I make a countdown timer in JavaScript?

Yes. Subtract the current time from your target date inside setInterval every second, and split the milliseconds into days, hours, minutes and seconds. The last section of this lesson shows a complete one.