Digital Clock
UI Coding
Easy
20 min
timers
effects
dates
accessibility
Build a digital clock that shows the current local time and updates every second.
Requirements
- Display the time as
HH:MM:SS(24-hour, zero-padded, e.g.09:05:03) inside a<time>element whosedateTimeattribute holds a machine-readable value. - A 24-hour toggle button (with
aria-pressed) switches between13:07:42and1:07:42 PM. In 12-hour mode, midnight is12:…:… AMand noon is12:…:… PM. - Show the date underneath ("Tuesday, 17 November 2026"). Any readable format is fine.
- The clock must tick on the second boundary. It shouldn't drift, lag up to a second behind the system clock, or skip seconds.
- Clean up timers on unmount.
- Don't announce every tick to screen readers.
Follow-up
- Why can
setInterval(tick, 1000)show the wrong second, or skip one? toLocaleTimeStringor manual formatting? What changes for international users?- How would you make the digits not jiggle as they change?
Spotted a mistake or a rights concern? Report an issue