Sub topic 2
Creating Dates and the Zero-Based Month Trap
This chapter assumes you know JavaScript basics, can run code in a browser console or Node.js, and know that a Date holds one millisecond time value since the Unix epoch. Here you will see the ways to create a Date and the mistakes each way invites.
The Four Constructor Forms
The Date constructor does different things depending on what you pass in.
| Call | Meaning | Time zone used |
|---|---|---|
| `new Date()` | The current instant | Not applicable |
| `new Date(ms)` | The instant `ms` milliseconds after the epoch | Not applicable |
| `new Date(string)` | Parse a string (covered in the next chapter) | Depends on the string's format |
| `new Date(y, m, d, h, min, s, ms)` | Build from calendar components | Local time |
const now = new Date(); // current instant
const epoch = new Date(0); // 1970-01-01T00:00:00.000Z
const local = new Date(2024, 6, 4, 9, 30); // July 4, 2024, 09:30 local time
const utcMs = Date.UTC(2024, 6, 4, 9, 30); // a number, not a Date
const utc = new Date(utcMs); // July 4, 2024, 09:30 UTC
console.log(typeof Date()); // "string": without new, you get text
console.log(utc.toISOString()); // "2024-07-04T09:30:00.000Z"The component form always uses the runtime's local time zone. To build an instant from UTC components, pass them to Date.UTC, which returns a millisecond value, and wrap that value in new Date.
Months Start at Zero
In the component form, the month argument runs from 0 (January) to 11 (December). Every other component, including the day, starts where you would expect. new Date(2024, 12, 25) gives you December 25, 2024 only if you write 11 as the month. With 12 you get January 25, 2025.
Out-of-Range Values Roll Over
Date does not reject values that are too large or too small. It carries the extra into the next unit. This can silently move a date:
const d = new Date(2024, 0, 31); // January 31, 2024
d.setMonth(1); // "February 31"
console.log(d.getMonth(), d.getDate()); // 2 2 -> March 2
// The same rollover can be useful: day 0 means "the last day of the previous month".
const lastOfFeb = new Date(2024, 2, 0); // March 0 = February 29, 2024
console.log(lastOfFeb.getDate()); // 29Two-Digit Years
When you pass a year from 0 to 99 to the component form, it becomes 1900 + year. To get a date that really is in year 5 or year 99, set the year afterwards:
console.log(new Date(99, 0, 1).getFullYear()); // 1999
const ancient = new Date(2000, 0, 1);
ancient.setFullYear(5);
console.log(ancient.getFullYear()); // 5Copying a Date
To make an independent copy, pass the original's time value to the constructor: new Date(original.getTime()). A later chapter shows why copying matters before you change a date.
Exercise
A form gives you the strings "2024", "12", and "25" for year, month, and day. Write the expression that creates December 25, 2024 at local midnight, and say what new Date(2024, 12, 25) would produce instead.
Show answer
new Date(Number("2024"), Number("12") - 1, Number("25")) creates December 25, 2024. Without the - 1, month 12 rolls over into the next year, so new Date(2024, 12, 25) gives January 25, 2025.
Exercise
Using rollover, write a function daysInMonth(year, month), where month is 1–12, that returns the number of days in that month.
Show answer
const daysInMonth = (year, month) => new Date(year, month, 0).getDate(); The 1-based month passed as a 0-based index points to the following month, and day 0 steps back to the last day of the month you asked for. daysInMonth(2024, 2) returns 29.