Go's time.ParseDuration in TypeScript

✓ Verified: Go 1.23
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { ParseDuration } from 'locutus/golang/time/ParseDuration'.

Or with CommonJS: const { ParseDuration } = require('locutus/golang/time/ParseDuration')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1ParseDuration('300ms')300
2ParseDuration('1h2m3.5s')3723500
3ParseDuration('-1.5h')-5400000
4ParseDuration('2h45m')9900000

Notes

  • Parses Go duration strings and returns milliseconds.

  • Supports ns, us/µs, ms, s, m, h units and chained segments.

Here's what our current TypeScript equivalent to Go's time.ParseDuration looks like.

const DURATION_UNIT_TO_MS: Record<string, number> = {
ns: 1e-6,
us: 1e-3,
'\u00b5s': 1e-3,
ms: 1,
s: 1_000,
m: 60_000,
h: 3_600_000,
}

const DURATION_PART_REGEX = /^(\d+(?:\.\d+)?|\.\d+)(ns|us|\u00b5s|ms|s|m|h)/

export function ParseDuration(s: string): number {
// discuss at: https://locutus.io/golang/time/ParseDuration
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Parses Go duration strings and returns milliseconds.
// note 2: Supports ns, us/µs, ms, s, m, h units and chained segments.
// example 1: ParseDuration('300ms')
// returns 1: 300
// example 2: ParseDuration('1h2m3.5s')
// returns 2: 3723500
// example 3: ParseDuration('-1.5h')
// returns 3: -5400000
// example 4: ParseDuration('2h45m')
// returns 4: 9900000

const raw = String(s).trim()
if (raw === '') {
throw new TypeError('ParseDuration(): invalid duration')
}

let sign = 1
let rest = raw

if (rest.startsWith('+') || rest.startsWith('-')) {
sign = rest[0] === '-' ? -1 : 1
rest = rest.slice(1)
}

if (rest === '0') {
return 0
}

let totalMs = 0

while (rest.length > 0) {
const match = DURATION_PART_REGEX.exec(rest)
if (!match) {
throw new TypeError('ParseDuration(): invalid duration')
}

const valueLiteral = match[1]
const unit = match[2]
if (!valueLiteral || !unit) {
throw new TypeError('ParseDuration(): invalid duration')
}

const value = Number.parseFloat(valueLiteral)
const multiplier = DURATION_UNIT_TO_MS[unit]

if (!Number.isFinite(value) || multiplier === undefined) {
throw new TypeError('ParseDuration(): invalid duration')
}

totalMs += value * multiplier
rest = rest.slice(match[0].length)
}

return sign * totalMs
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


We have 47 Go functions so far - help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.


« More Go time functions


Star