Building Dates With new Date()

Larkspur Physio’s reminder script checks which appointments fall in the coming week. For the Thursday run, the Python script starts at eight in the morning on October 29, 2026, in Coordinated Universal Time (UTC) and keeps five appointments. You’ll port that starting point to JavaScript.

First, you’ll use Date, the built-in date type you’ll meet in older JavaScript code, libraries, and answers online. You need to recognize its conventions when reading that code. Temporal, the modern date and time application programming interface (API), takes over in the course’s next section.

Count the Coming Week’s Appointments

For this count, only the export’s start timestamps matter. Here are all six, in export order, with the Python function that keeps the coming week’s appointments:

Language: Python
from datetime import UTC, date, datetime, timedelta

starts = [
    "2026-11-01T15:00:00Z", "2026-10-30T09:30:00Z",
    "2026-11-02T16:00:00Z", "2026-11-03T11:00:00Z",
    "2026-10-30T17:00:00Z", "2026-11-05T09:00:00Z",
]
appointments = [datetime.fromisoformat(s) for s in starts]

def main(now):
    week_end = now + timedelta(days=7)
    week = [start for start in appointments
            if now <= start < week_end]
    print(f"{len(week)} appointments this week")

main(datetime(2026, 10, 29, 8, 0, tzinfo=UTC))

The Thursday run prints:

Language: Program Output
5 appointments this week

Jonas’s appointment on November 5 at 09:00 UTC falls after the week’s end at 08:00. The other five belong in the report.

The JavaScript excerpt below keeps the same six start fields and converts each string into a Date. The filter replaces Python’s chained comparison with two comparisons joined by &&. Dates support these comparisons because JavaScript converts them to numbers.

Create a reminders folder in your home folder (~/reminders). Inside it, save a file named package.json with this content to make the JavaScript files ECMAScript (ES) modules:

Language: JSON
{ "type": "module" }

Save the JavaScript below as reminders.js in the same folder. Open a terminal there. With Node installed, run the file with node reminders.js.

The first line, process.env.TZ = "Europe/London", overrides the local time zone for this Node process, so you can reproduce Isla’s London setting on any system. The last line carries over Python’s numeric date parts. Predict how many appointments the script will keep before running it:

Language: JavaScript
process.env.TZ = "Europe/London";
const EXPORT_TEXT = `[
  {"start": "2026-11-01T15:00:00Z"},
  {"start": "2026-10-30T09:30:00Z"},
  {"start": "2026-11-02T16:00:00Z"},
  {"start": "2026-11-03T11:00:00Z"},
  {"start": "2026-10-30T17:00:00Z"},
  {"start": "2026-11-05T09:00:00Z"}
]`;
const appointments = JSON.parse(EXPORT_TEXT).map((appt) => ({
  ...appt, start: new Date(appt.start),
}));
const WEEK_MS = 7 * 24 * 60 * 60 * 1000;

function main(now) {
  const weekEnd = new Date(now.getTime() + WEEK_MS);
  const week = appointments.filter((appt) =>
    now <= appt.start && appt.start < weekEnd
  );
  console.log(`${week.length} appointments this week`);
}

main(new Date(2026, 10, 29, 8, 0));

JavaScript finishes without an error but prints:

Language: Program Output
0 appointments this week

The appointments haven’t disappeared. The starting date changed.

Read the Date You Built

Keep a copy of the original reminders.js script so you can return to its filter later. For this separate check, replace the entire contents of reminders.js with the following short example, and then run node reminders.js again. It asks the constructed Date to show its moment in UTC with .toISOString():

Language: JavaScript
process.env.TZ = "Europe/London";
const now = new Date(2026, 10, 29, 8, 0);
console.log(now.toISOString());

The string reveals the month:

Language: Program Output
2026-11-29T08:00:00.000Z

The numeric constructor counts months from zero. January is zero, so ten means November. That’s a valid month, so JavaScript has no error to report. Your filter looked a month ahead of the clinic’s export.

A valid Date stores one moment as milliseconds since the start of 1970 in UTC. .getTime() reads that number, which is why adding WEEK_MS builds the filter’s end moment. Date.now() gives the current moment as the same kind of number. This report uses a fixed week of 168 elapsed hours in UTC, which can differ from seven local calendar days across a clock change.

To inspect that wrong month directly, use now.month in Python or now.getMonth() in JavaScript. JavaScript’s local-time getters read calendar fields in the Node process’s local time zone, set by process.env.TZ. Their UTC counterparts read UTC fields. Python’s .year, .day, and .hour correspond to .getFullYear(), .getDate(), and .getHours().

Callum’s New York setting exposes another difference: the numeric constructor interprets its parts as local time too. Overwrite reminders.js with this check, then rerun the file to inspect the month and hour alongside the UTC string:

Language: JavaScript
process.env.TZ = "America/New_York";
const now = new Date(2026, 10, 29, 8, 0);
console.log("Local month:", now.getMonth());
console.log("Local hour:", now.getHours());
console.log("UTC:", now.toISOString());

The fields and string describe the same Date:

Language: Program Output
Local month: 10
Local hour: 8
UTC: 2026-11-29T13:00:00.000Z

The local month is still November, numbered ten. The local hour is eight, but .toISOString() shows thirteen in UTC. Local getters tell Callum what his clock reads, while the UTC string lets you compare the run with the export’s timestamps. Eight in New York is a different moment from eight in London. Python’s tzinfo=UTC made the original call independent of that setting.

On this Date, .getFullYear() gives 2026 and .getDate() gives 29. .getDay() gives 0 for Sunday, whereas Python’s .weekday() counts from Monday as zero.

Choose an Explicit UTC Moment

For the clinic’s fixed run, use an ISO 8601 timestamp with Z, meaning UTC. Its written month counts from one, as in Python’s constructor. Replace the entire contents of reminders.js with this next example and run node reminders.js again:

Language: JavaScript
const now = new Date("2026-10-29T08:00:00Z");
console.log(now.toISOString());

On either laptop, this prints:

Language: Program Output
2026-10-29T08:00:00.000Z

Now restore your saved copy of the original reminders.js script, including its export and filter. Replace only its last line with main(new Date("2026-10-29T08:00:00Z")); and run node reminders.js again. The unchanged filter now prints 5 appointments this week in both time zones. The string identifies one moment, and the machine’s setting no longer changes which appointments the filter keeps.

Two other Date conventions matter when you maintain that older code. A day beyond the month’s end rolls forward, so new Date(2026, 1, 30) means March 2, whereas Python’s date(2026, 2, 30) raises ValueError.

A Date also changes in place, unlike Python’s immutable datetime: after const start = new Date("2026-10-29T08:00:00Z"); and const end = start;, calling end.setDate(end.getDate() + 1) moves start too, because both names refer to the same object.

The clinic’s run now starts at the intended moment. Next up is Reading Date Strings With Date. There, you’ll read the export’s appointment timestamps and its calendar-only closed day and check what each string means on the two laptops.

Become a Member to join the conversation.