Skip to content

Utilities ​

parseDuration ​

ts
parseDuration(duration: string, strict?: boolean): number

Parses a duration string such as '1h 30m' and returns milliseconds. See Duration strings for the format.

ParameterTypeDefaultDescription
durationstringThe string to parse.
strictbooleanfalseThrow an Error for an invalid string, instead of returning 0.

clockToMs ​

ts
clockToMs(clock: string): number

Converts a clock string in the format hh:mm:ss.ms_μs_ns to milliseconds. Only the seconds are required. Prefix it with - for a negative duration. See Clock strings.

PartMeaningNotes
hhHoursOptional; leading zero optional
mmMinutesOptional; leading zero optional
ssSecondsRequired; leading zero optional
msMillisecondsOptional
μsMicrosecondsOptional
nsNanosecondsOptional

Separate milliseconds, microseconds and nanoseconds with underscores, or pad each to three digits.

msToClock ​

ts
msToClock(ms: number, options?: { separateDecimal?: boolean }): string

Formats milliseconds as hh:mm:ss, adding .ms_μs_ns when there's a fraction of a second. Negative durations start with -. See Clock strings.

OptionTypeDefaultDescription
separateDecimalbooleantrueSeparate milliseconds, microseconds and nanoseconds with underscores (_).

Days and weeks aren't included: the hours wrap after 23.

msToParts ​

ts
msToParts(ms: number): Parts

Splits milliseconds into a Parts object, with what's left of each unit after the larger ones. See Parts.

partsToMs ​

ts
partsToMs(parts: Partial<Parts>): number

Joins a Parts object into milliseconds. Every property is optional, and isNegative defaults to false. See Parts.