Making dates and reading their parts
fromParts === d is false: they are two different objects holding the same moment. "15/01/2024" isn't ISO, and Chrome can't read it. new Date(2024, 0, 1) uses local time, but its month is 0 everywhere.
A Date stores a single moment as the number of milliseconds since 1 January 1970 UTC (a timestamp), and shows it either on the computer's local clock or in UTC. Months count from 0, setters change the object, parsing only works reliably for ISO strings, and the new Temporal API fixes most of these old problems.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write daysBetween(a, b) that takes two ISO date strings like "2024-01-15" and returns the whole number of days between them.
Write isWeekend(isoString) that returns true for Saturdays and Sundays, judged in UTC.
Write ageOn(birthIso, onIso) that returns a person's age in whole years on a given date.
Explain it without notes
What does a Date object actually store?
Why do months start at 0, and where does that bite?
How should dates be stored in a database or sent in an API, and how should they be displayed?
How do you safely add days to a date and count the days between two dates?
Preview
Console
Nothing logged yet.Expected console output
1705314600000 2024-01-15T10:30:00.000Z
2024 0 15 1 10
true false
1970-01-01T00:00:00.000Z 0
2024-01-15T00:00:00.000Z
Mon 15 Jan 2024
Invalid Date NaN true
number true