Adding Up Booked Time With Duration

Larkspur Physio’s reminders now land a calendar day before each session. The weekly report also needs each clinician’s booked time: one hour and forty minutes for Isla Grant and one hour and twenty minutes for Callum Reid. Adding the minutes gives you the correct totals, but displaying them exposes another difference from Python.

Add the Booked Minutes

For the Thursday, October 29, run at 08:00 in London, four sessions contribute to those totals. Fiona’s Tuesday session is on the closed day, and Jonas’s Thursday, November 5, session is outside the week. The examples below take the clinician and minutes fields from the four contributing sessions in the six-record export and group the sessions in report order.

In Python, save this standalone calculation as booked.py in your reminders folder and run python booked.py:

Language: Python
from datetime import timedelta

sessions = [
    {"clinician": "Isla Grant", "minutes": 50},
    {"clinician": "Isla Grant", "minutes": 50},
    {"clinician": "Callum Reid", "minutes": 50},
    {"clinician": "Callum Reid", "minutes": 30},
]
booked = {}
for appt in sessions:
    clinician = appt["clinician"]
    booked[clinician] = booked.get(clinician, timedelta()) + (
        timedelta(minutes=appt["minutes"])
    )

for clinician, total in booked.items():
    print(f"{clinician}: {total} booked")

Python’s printed durations already use hours and minutes:

Language: Program Output
Isla Grant: 1:40:00 booked
Callum Reid: 1:20:00 booked

In JavaScript, new Temporal.Duration() supplies a zero duration, like timedelta(). A duration represents an amount of time rather than a date on the calendar. Its .add({ minutes: ... }) returns a new duration, so you store that result back in the clinician’s entry.

A Map holds those entries. Its .get() returns undefined for a missing clinician, and ?? supplies a zero duration only when the result is null or undefined. The for...of loop reads each session, while [clinician, total] unpacks each map entry in the display loop.

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.