@credentum/seconds-to-hhmmss
v0.0.1
Published
Convert seconds to HH:MM:SS time strings and back. Zero deps. Handles >24h.
Maintainers
Readme
@credentum/seconds-to-hhmmss
Convert seconds to HH:MM:SS time strings and back. Zero dependencies. Handles durations over 24 hours.
Installation
npm install @credentum/seconds-to-hhmmssUsage
import { secondsToHms, hmsToSeconds } from '@credentum/seconds-to-hhmmss';
secondsToHms(3661); // '01:01:01'
secondsToHms(90); // '00:01:30'
secondsToHms(86400); // '24:00:00' (no 24h wrap bug)
hmsToSeconds('01:30:00'); // 5400
hmsToSeconds('100:00:00'); // 360000
hmsToSeconds('05:30'); // 330Why Use This?
The classic one-liner new Date(s * 1000).toISOString().slice(11, 19) wraps at 24 hours. pretty-ms takes milliseconds and outputs words. hh-mm-ss hasn't been updated since 2017.
This package:
- Takes seconds (not milliseconds)
- Handles durations over 24 hours without wrapping
- Converts both directions (seconds ↔ HH:MM:SS)
- Ships TypeScript declarations
- Has zero dependencies
- Is under 1KB
API
secondsToHms(seconds: number): string
Converts a non-negative number of seconds to a zero-padded HH:MM:SS string.
| Input | Output |
|-------|--------|
| 0 | '00:00:00' |
| 59 | '00:00:59' |
| 3600 | '01:00:00' |
| 86399 | '23:59:59' |
| 86400 | '24:00:00' |
| 360000 | '100:00:00' |
Throws RangeError for negative numbers or non-finite values. Fractional seconds are floored.
hmsToSeconds(time: string): number
Parses a time string back to seconds. Accepts HH:MM:SS or MM:SS format.
| Input | Output |
|-------|--------|
| '00:00:00' | 0 |
| '01:30:00' | 5400 |
| '05:30' | 330 |
| '100:00:00' | 360000 |
Throws TypeError for invalid format. Throws RangeError for out-of-range components (minutes or seconds >= 60).
License
MIT
