Comparing and Sorting Appointments

The booking system lists appointments in booking order, so Dana’s Sunday session comes before Fiona’s Friday session in the export. Larkspur Physio needs the coming week’s appointments in time order before it can print reminders.

You can now turn the exported timestamps into zoned values. The next step is to keep appointments from Thursday, October 29 at 08:00 in London up to but not including the same time the following Thursday. Five of the six appointments belong in that window. Start with the Python filter that already works.

Set the Week’s Boundaries

Python’s aware datetimes support ordering, and timedelta supplies the week’s length. This view keeps only the patient and start fields from the six exported records:

Language: Python
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo

CLINIC_ZONE = ZoneInfo("Europe/London")
WEEK = timedelta(days=7)
appointments = [
    {"patient": "Dana Whitfield",
     "start": "2026-11-01T15:00:00Z"},
    {"patient": "Fiona Mackay",
     "start": "2026-10-30T09:30:00Z"},
    {"patient": "Jonas Becker",
     "start": "2026-11-02T16:00:00Z"},
    {"patient": "Fiona Mackay",
     "start": "2026-11-03T11:00:00Z"},
    {"patient": "Dana Whitfield",
     "start": "2026-10-30T17:00:00Z"},
    {"patient": "Jonas Becker",
     "start": "2026-11-05T09:00:00Z"},
]
appointments = [
    {**appt, "start": datetime.fromisoformat(appt["start"])}
    for appt in appointments
]
now = datetime(2026, 10, 29, 8, tzinfo=CLINIC_ZONE)
week = [
    appt for appt in appointments
    if now <= appt["start"] < now + WEEK
]
week.sort(key=lambda appt: appt["start"])
print([appt["patient"] for appt in week])

The lower boundary includes an appointment starting now. The upper boundary excludes one starting exactly a week later. Keep those same boundaries in JavaScript.

The supplied UTC start times already avoid Python’s repeated-hour issue: datetimes in the same local zone can compare equal while representing different moments. If you store start times in a local zone, convert them to UTC before ordering. Temporal’s comparison orders moments regardless of their zones.

Use a scratch file named compare.js in your existing ~/reminders project, where package.json already sets "type": "module". Run it with node compare.js from that folder. For each independent JavaScript example below, replace the entire contents of compare.js before running it. Run the final complete example separately from the example that intentionally fails.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.