Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 4.3 — Dates and Time Zones
Data Visualization · Example 3 of 3

Parse, format and sort dates

Two silent failures: the impossible date 2025-03-32 becomes 1 April, and the default sort puts 30 March first.

A timestamp is one exact instant, but the calendar day it falls on depends on whose clock you read. Parse dates with a stated format, format them on a stated clock, and never let a chart silently shift a ride to the wrong day.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write localDay(iso, zone) returning YYYY-MM-DD. Use it to count how many of trips-10k.csv's trips start on 15 June on the Pacific/Auckland clock.

02

Write hoursInLocalDay(dateText, zone) that returns 23, 24 or 25 for a YYYY-MM-DD date by counting the hours.

Explain it without notes

01

Why can a trip at 00:30 UTC belong to the previous day for some viewers?

02

How do you get the local day of an instant for Ardenvale without a library?

03

Why is dividing a daily total by 24 wrong on 30 March 2025?

Parse, format and sort dates

Console

(nothing was printed)

Expected console output

utcParse('13/03/2025'): 2025-03-13T00:00:00.000Z
utcParse('2025-03-32'): 2025-04-01T00:00:00.000Z
utcParse('2025-03-12 '): null
new Date('13/03/2025'): Invalid Date
utcFormat: Wednesday 18 June 2025, 08:15
Ardenvale: Wednesday, 18 June 2025 at 08:15
Auckland:  Wednesday, 18 June 2025 at 20:15
default sort() : 2025-03-30 2025-03-04 2025-03-12
d3.ascending   : 2025-03-04 2025-03-12 2025-03-30
ISO text sorts too: 2025-03-04 2025-03-12 2025-03-30
but this text does not: 04/03/2025 13/03/2025 30/01/2025