Skip to content

Converting units ​

Each time unit has a converter. Call it with an amount to get milliseconds:

ts
minutes
(2) +
seconds
(30);
// => 150_000
hours
(1.5);
// => 5_400_000
weeks
(2);
// => 1_209_600_000 const
expiresAt
=
Date
.
now
() +
days
(30);

Amounts can be fractional or negative, and converters can be added together for compound durations.

Converting back ​

Every converter has a .from() method that turns milliseconds back into that unit:

ts
minutes
.
from
(150_000);
// => 2.5
hours
.
from
(5_400_000);
// => 1.5
days
.
from
(
weeks
(1));
// => 7

Combine a converter and .from() to convert between any two units.

Units ​

ConverterOne unit in milliseconds
weeks604_800_000
days86_400_000
hours3_600_000
minutes60_000
seconds1_000
milliseconds1
microseconds0.001
nanoseconds0.000_001

Sub-millisecond units produce fractions of a millisecond:

ts
microseconds
(1);
// => 0.001
nanoseconds
(500);
// => 0.0005

milliseconds returns its input unchanged. See why it exists.