Browse the docs

parseDuration

Reads a length of time such as 1h 30m, 90 min, 1:30:00 or PT1H30M as seconds.

Details #

parseDuration(text) takes one argument and returns a number of seconds.

  • A bare number is seconds: 90.

  • Clock form: 1:30 is minutes and seconds, 1:30:00 hours, minutes and seconds.

  • ISO 8601: PT1H30M, P2DT3H.

  • Words and short forms, in any order: 1h 30m, 1h30m, 90 min, 2 days 3 hours, 1.5 hours, 1 hr, 15 mins, 2w, 45s.

Units are s, sec, second; m, min, minute; h, hr, hour; d, day; w, wk, week (plural forms too). Here a bare m means minutes.

Parameters #

Takes 1 argument. {{ parseDuration(text) }}

Examples #

parseDuration("1h 30m")
→ 5400
parseDuration("1:30:00")
→ 5400
parseDuration("2 days 3 hours")
→ 183600

Gotchas #

  • Text that isn't a length of time is an error: parseDuration() couldn't read a length of time from 'soon'.

  • Months and years are not units: their length varies.