Quiz Entry - updated: 2026.06.23
How do you compute and display a train's delay from the transport API's response?
You subtract the scheduled departure time from the API's predicted ("prognosis") time and convert that millisecond difference into minutes.
The API gives two timestamps per departure, and the delay is simply the gap between them:
stop.departure— the scheduled time.stop.prognosis.departure— the predicted actual time, present only when a real-time prediction exists.
let departure = new Date(current.stop.departure);
if (current.stop.prognosis.departure) {
let prognosis = new Date(current.stop.prognosis.departure);
let delayMs = prognosis - departure; // Date subtraction → milliseconds
let delayMin = Math.round(delayMs / 60000); // 60000 ms = 1 minute
if (delayMin > 0) {
// surface it prominently, e.g. bold/red: '+' + delayMin + ' min'
}
}
Two things to notice. First, you must guard on prognosis.departure existing — many departures have no live prediction, and reading times off undefined would throw. Second, subtracting two Date objects yields milliseconds, so you divide by 60000 to get minutes.
Tip: Show delays prominently (bold, red) since that's the information a waiting traveller actually scans for — an on-time row needs no decoration.