Dates
JavaScript · Unit 17: What JavaScript brings along
- Full plan
- JavaScript
- 10 exercises
A date is built with new Date(year, month, day). Watch the month: it's counted from 0, so January is 0 and September is 8.
A date doesn't print nicely: you ask it for its parts.
const d = new Date(2026, 8, 22);
console.log(d.getFullYear());
console.log(d.getMonth());
console.log(d.getDate());Prints
2026 8 22
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
The date is January 31st, 2026. What does it print?
2. Complete the code
Fill in the blank so
dis December 25th, 2026.3. Complete the code
Fill in the blank in
isWeekend, which tells whether that 2026 date falls on Saturday or Sunday.4. Predict the output
How many days are there between the two dates?
5. Complete the code
Fill in the blank so
daysis the difference in days.6. Predict the output
Both dates are the same day. What does it print?
7. Find the bug
This program should print 25/12/2026. Which line has the error?
8. Find the case that fails
duration(day1, day2)gives how many days there are between two days of June 2026, no matter which one is written first: it never gives a negative number. Which call breaks it?9. Put the lines in order
Put
days(day)in order: how many days pass from June 1st, 2026 to that day of June.10. Multiple choice
You want to test a function that works with dates. Why do you hand it a fixed date instead of using
new Date()inside?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.