Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Sending Reminders a Day Ahead

The clinic sends a reminder on the previous day at the patient’s appointment time. Dana’s Sunday session is at 10:00 in New York. The reminder needs to be Saturday at 10:00, even though the clocks go back between those two times.

Python’s start - timedelta(hours=24) on the aware datetime uses local calendar and clock fields. It gives Saturday at 10:00. Python stores timedelta(days=1) and timedelta(hours=24) as the same value, so changing the spelling doesn’t change the result.

Temporal distinguishes the two requirements:

  • { days: 1 } moves to the previous calendar day at the same local time in this example.
  • { hours: 24 } counts exactly twenty-four elapsed hours.

For Dana’s appointment, twenty-four elapsed hours earlier is Saturday at 11:00. Saturday 10:00 to Sunday 10:00 spans twenty-five elapsed hours because New York repeats an hour on Sunday 1 November. Choose calendar units for the clinic’s promise, and do the arithmetic on the start in the patient’s named zone.

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

00:00 The clinic promises each patient a reminder the day before, at the same time of day. You’ll port that calculation for Dana’s Sunday appointment with Callum in New York.

00:09 The appointment is at ten in the morning. In Python, the exported timestamp is converted into Dana’s zone before the subtraction. The lead is a timedelta written as twenty-four hours.

00:23 Python prints Saturday at ten for the reminder. Keep that appointment and the clinic’s promise in view as you write the JavaScript.

00:33 In JavaScript, the timestamp becomes an Instant, then a zoned value for New York.

00:41 These lines keep the same appointment you already have in Python. .subtract() takes a units object, like .add(), and returns a new value.

00:52 Copy the Python lead as twenty-four hours and keep the result as the reminder.

00:59 What reminder time does this JavaScript print for Dana? Your turn: pause the video and predict the time.

01:11 Eleven on Saturday. That’s an hour later than the clinic’s promise, with no error. The start is still Sunday’s ten o’clock appointment. The difference is in the meaning of those hours.

01:25 New York’s clocks go back on Sunday the first of November. The local clock passes one fifty-nine, then reads one o’clock again, with a different offset. The interval from Saturday at ten to Sunday at ten contains that extra hour.

01:41 It lasts twenty-five elapsed hours. Temporal’s hours count actual elapsed time. Follow twenty-four hours back from Sunday’s appointment and you land at Saturday eleven.

01:55 The exported appointment hasn’t changed. The patient’s New York clock is the clock the reminder rule has to follow. Why did Python’s twenty-four hours give you ten?

02:07 A timedelta for one day and a timedelta for twenty-four hours are the same value. Python subtracts that value from the local calendar and clock fields, keeping the zone.

02:18 So Sunday ten becomes Saturday ten, even though twenty-five elapsed hours separate them. The Python script already meets the clinic’s promise. Changing the timedelta’s spelling to days would give the same result.

02:33 Its stored value doesn’t remember which spelling you used. The JavaScript port needs to express that calendar meaning explicitly.

02:44 Use a calendar day for the clinic’s rule. Change the lead to one day in the JavaScript units object.

02:52 On this zoned appointment, subtraction chooses the previous calendar day at the same local time. The result is Saturday at ten, matching Python and the promise.

03:04 That interval still contains twenty-five elapsed hours. The patient’s zone supplies the clock rules for that date. Watch out: subtraction leaves the start unchanged, unlike Date’s setters, so keep the returned value.

03:19 For an exactly twenty-four-hour requirement, keep hours instead. The units say which promise your code is making. For the clinic’s reminder, choose a calendar day on the appointment in the patient’s zone.

03:35 That gives you the same clock time on the previous day. Choose hours when the requirement is an exact elapsed interval. The two choices can agree on ordinary dates and differ across a clock change.

03:49 Dana’s Sunday is the case that made the distinction visible. Next up is Adding Up Booked Time With Duration. You’ll total the time Isla and Callum have booked, then measure how long remains before an appointment.

Become a Member to join the conversation.