Menu

Parsing Run Times in a Calculator: mm:ss, Seconds, and the Ties That Cost Points
📰

Parsing Run Times in a Calculator: mm:ss, Seconds, and the Ties That Cost Points

DEV Community: javascript·Leene Casella·15 days ago
#D3FsTAgb
#devtojavascript#devto#dev#seconds#null
Reading 0:00
15s threshold

Any calculator that scores a timed event has the same quiet problem: people type times in five different ways. One person types 14:30 . Another types 870 . A third types 14.30 and means fourteen minutes and thirty seconds. If your parser guesses wrong, the score is wrong, and the user usually cannot tell.

This post walks through a small, boring approach that has held up well for me when building fitness-test calculators: accept a few clearly defined formats, reject everything else with a helpful message, and compare times as whole seconds.

Decide what you accept, and write it down

Before writing code, list the formats you will accept. Mine are:

mm:ss such as 14:30 or 9:05

m:ss with a single-digit minute

h:mm:ss for very long events, such as 1:02:15

plain seconds such as 870 , only when the field is labelled as seconds

Everything else is an error. That includes 14.30 , 14,30 , and 14m30s .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.