JavaScript · Lesson 18 / 32 · English
JavaScript Dates
Understand the idea. Change the code. Build confidence with one focused experiment.
Read this lesson in தமிழ் ↗.
Start with the idea
A Date represents an instant in time. Local display depends on the environment, so use explicit UTC values when you need a predictable example.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
Step-by-step lesson notes
Date Object in JavaScript
The Date object is used to work with dates and times. Internally it stores time as the number of milliseconds since January 1, 1970, 00:00:00 UTC (Unix epoch).
Creating Dates
const now = new Date(); // current date & time
const specific = new Date("2024-01-15"); // from ISO string
const fromParts = new Date(2024, 0, 15); // year, month(0-indexed), day
const fromMs = new Date(0); // Unix epoch: Jan 1, 1970
console.log(Date.now()); // current time as millisecondsGetting Date Components
const d = new Date();
d.getFullYear(); // 2024
d.getMonth(); // 0-11 (0 = January!)
d.getDate(); // 1-31 (day of month)
d.getDay(); // 0-6 (0 = Sunday)
d.getHours(); // 0-23
d.getMinutes(); // 0-59
d.getSeconds(); // 0-59
d.getMilliseconds(); // 0-999
d.getTime(); // milliseconds since epochWatch out! Months are 0-indexed: January = 0, December = 11.
Formatting Dates
const d = new Date();
d.toISOString(); // "2024-01-15T10:30:00.000Z"
d.toLocaleDateString(); // "1/15/2024" (locale-dependent)
d.toLocaleTimeString(); // "10:30:00 AM"
d.toLocaleString(); // "1/15/2024, 10:30:00 AM"
// Custom formatting with Intl.DateTimeFormat
const formatter = new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "long",
day: "numeric"
});
console.log(formatter.format(d)); // "January 15, 2024"Date Arithmetic
const start = new Date("2024-01-01");
const end = new Date("2024-12-31");
// Difference in days
const diffMs = end - start; // milliseconds
const diffDays = diffMs / (1000 * 60 * 60 * 24);
console.log(diffDays); // 365
// Add 7 days
const nextWeek = new Date();
nextWeek.setDate(nextWeek.getDate() + 7);A complete example
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
const lessonDate = new Date("2026-01-15T10:00:00Z");
console.log(lessonDate.toISOString());
console.log(lessonDate.getUTCFullYear());
console.log(lessonDate.getUTCMonth() + 1);Predict first. Reveal the expected result.
2026-01-15T10:00:00.000Z 2026 1
Make one change. See what happens.
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Wait for the editor to load, then load this example and press Run.
Powered by the free OneCompiler editor. Code you run is sent to that external service. Keep personal data and secret keys out of your examples.
Put the idea to work
Practice challenge
Change the timestamp to 2026-02-01T00:00:00Z.
Reveal the answer and reasoning
The UTC month plus one becomes 2 and the ISO output is 2026-02-01T00:00:00.000Z.
One mistake to avoid: getMonth and getUTCMonth are zero-based. Avoid ambiguous date strings such as 01/02/2026.
Your learning checkpoint
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.