z-time-units
v1.0.0
Published
Tiny, dependency-free TypeScript library for converting between fixed units of elapsed time
Downloads
163
Readme
time-units
A tiny, dependency-free TypeScript library for converting between fixed units of elapsed time: milliseconds, seconds, minutes, hours, and days.
The entire implementation is a single TypeScript file:
index.ts.
Usage
import {
HOURS_PER_DAY,
daysToHours,
hoursToMinutes,
millisecondsToSeconds,
minutesToMilliseconds,
} from "z-time-units";
minutesToMilliseconds(5); // 300_000
millisecondsToSeconds(1_500); // 1.5
daysToHours(2); // 48
hoursToMinutes(1.5); // 90
HOURS_PER_DAY; // 24API
Constants
Every smaller-per-larger relationship among the supported units:
| Constant | Value |
| ------------------------- | ------------ |
| MILLISECONDS_PER_SECOND | 1_000 |
| MILLISECONDS_PER_MINUTE | 60_000 |
| MILLISECONDS_PER_HOUR | 3_600_000 |
| MILLISECONDS_PER_DAY | 86_400_000 |
| SECONDS_PER_MINUTE | 60 |
| SECONDS_PER_HOUR | 3_600 |
| SECONDS_PER_DAY | 86_400 |
| MINUTES_PER_HOUR | 60 |
| MINUTES_PER_DAY | 1_440 |
| HOURS_PER_DAY | 24 |
Conversion functions
One function for every ordered pair of distinct units, each with the
signature (value: number) => number:
millisecondsToSecondsmillisecondsToMinutesmillisecondsToHoursmillisecondsToDayssecondsToMillisecondssecondsToMinutessecondsToHourssecondsToDaysminutesToMillisecondsminutesToSecondsminutesToHoursminutesToDayshoursToMillisecondshoursToSecondshoursToMinuteshoursToDaysdaysToMillisecondsdaysToSecondsdaysToMinutesdaysToHours
Each performs a single multiplication or division against one of the constants above. Fractional, negative, and zero values flow through ordinary JavaScript arithmetic:
minutesToSeconds(1.5); // 90
secondsToMinutes(30); // 0.5
daysToHours(-2); // -48Semantics
- A
dayis exactly 24 hours. No calendars, timezones, or daylight-saving transitions. - Values are plain
numbers in and out. No validation, coercion, rounding, parsing, or formatting. - Dates, timestamps, structured durations, and calendar operations belong in higher-level libraries.
Contributing
See the CONTRIBUTING guide for setup and development scripts.
Packaging
Published as ESM-only with bundled type declarations, built into dist/
by npm run build. Runtime-agnostic (browsers, Node, Bun, Deno,
workers), with sideEffects: false so bundlers can tree-shake unused
exports.
License
MIT — unlike the rest of the surrounding monorepo, which is GPL-3.0-or-later, this package is MIT-licensed to encourage adoption.
