Skip to content

convertTimeUnit ​

The convertTimeUnit function converts values between different time units, such as milliseconds, seconds, minutes, hours, and days.

Syntax ​

typescript
convertTimeUnit(value: number, from: string, to: string): number;

Parameters ​

NameTypeDescription
valuenumberValue to be converted
fromstringSource unit (ms, s, min, h, d)
tostringTarget unit (ms, s, min, h, d)

Returns ​

TypeDescription
numberValue converted to the target unit

Examples ​

typescript
convertTimeUnit(2, 'h', 'min');
// => 120

convertTimeUnit(3600, 's', 'h');
// => 1

Notes ​

  • Throws an error if the units are invalid.
  • Useful for calculations involving different time scales.

References ​

Released under the MIT License.