JavaScript has a built-in Date object for working with dates and times. It's a bit quirky (it's one of the oldest parts of the language), but you'll definitely need it.
Creating dates
const now = new Date();
console.log(now); // the current date and time, right now
You can also create a specific date. Pay close attention to the month:
const moonLanding = new Date(1969, 6, 20); // year, month, day
console.log(moonLanding.getFullYear());
console.log(moonLanding.getMonth());
console.log(moonLanding.getDate());
console.log(moonLanding.getDay());
It prints
1969 6 20 0
The first Moon landing was on the 20th of July 1969. But we wrote 6 for the month?!
In JavaScript dates, months are numbered from 0 to 11: January is 0, February is 1, July is 6, and December is 11. Days of the month are normal (1 to 31). And getDay() gives the day of the week, where Sunday is 0, Monday is 1, and so on (so 0 means Sunday). This catches out absolutely everyone, including people who've been programming for twenty years. When working with dates, always double-check your months!
| Method | Gives you |
|---|---|
getFullYear() | The year, like 2026 |
getMonth() | The month, 0 to 11 |
getDate() | The day of the month, 1 to 31 |
getDay() | The day of the week, 0 (Sunday) to 6 (Saturday) |
getHours(), getMinutes(), getSeconds() | The time |
Date math
Under the hood, every date is stored as the number of milliseconds since the 1st of January 1970. (That moment is called the "Unix epoch," and it's the computer world's official birthday.) That means you can subtract two dates to find the difference in milliseconds, and then convert that into days:
const today = new Date(2026, 9, 15); // 15 October 2026
const christmas = new Date(2026, 11, 25); // 25 December 2026
const msPerDay = 1000 * 60 * 60 * 24;
const daysLeft = Math.round((christmas - today) / msPerDay);
console.log(`Only ${daysLeft} days until Christmas! 🎄`);
It prints
Only 71 days until Christmas! 🎄
(We use Math.round because daylight saving time in some countries can make a "day" slightly shorter or longer than 24 hours.)
Date.now() gives the current time in milliseconds. It's perfect for measuring how long something takes:
const start = Date.now();
// ... do something slow ...
const elapsed = Date.now() - start;
console.log(`That took ${elapsed} milliseconds.`);
Displaying dates nicely
To show a date to humans, use toLocaleDateString, which formats it the way people in a particular country expect:
const date = new Date(2026, 2, 6);
console.log(date.toLocaleDateString("en-GB")); // 06/03/2026
console.log(date.toLocaleDateString("en-US")); // 3/6/2026
console.log(date.toLocaleDateString("en-GB", { weekday: "long", day: "numeric", month: "long", year: "numeric" }));
// Friday, 6 March 2026
(The exact output can vary slightly between browsers.) Notice that the British and American formats put the day and month in a different order. Dates are surprisingly tricky! For serious date work, many developers use a helper library, which you'll learn how to install in College.