@scuton/ms-convert
v1.0.0
Published
Convert between human-readable time strings and milliseconds. Bidirectional.
Maintainers
Readme
@scuton/ms-convert
Convert between human-readable time strings and milliseconds. Bidirectional.
Installation
npm install @scuton/ms-convertUsage
import { toMs, toHuman } from '@scuton/ms-convert';
// String to milliseconds
toMs('2h 30m'); // 9000000
toMs('1d 12h'); // 129600000
toMs('1.5h'); // 5400000
toMs('500ms'); // 500
toMs('1mo'); // 2592000000
toMs('1y'); // 31536000000
// Milliseconds to human-readable string
toHuman(9000000); // '2h 30m'
toHuman(129600000); // '1d 12h'
toHuman(90061000); // '1d 1h 1m 1s'
// Short format (largest unit only)
toHuman(90061000, { short: true }); // '1d'
// Long format (full words)
toHuman(3600000, { long: true }); // '1 hour'
toHuman(7200000, { long: true }); // '2 hours'API
toMs(input: string): number
Converts a human-readable time string to milliseconds.
- Supports compound strings like
'2h 30m' - Supports decimal values like
'1.5h' - Case insensitive
- Throws
Errorif no valid time units are found
toHuman(ms: number, options?: ToHumanOptions): string
Converts milliseconds to a human-readable string.
Options
| Option | Type | Default | Description |
|---------|-----------|---------|--------------------------------------|
| short | boolean | false | Return only the largest unit |
| long | boolean | false | Use full unit names (e.g., "hours") |
Supported Units
| Unit | Short | Long | Milliseconds |
|------|-------|--------------|--------------------|
| ms | ms | millisecond | 1 |
| s | s | second | 1,000 |
| m | m | minute | 60,000 |
| h | h | hour | 3,600,000 |
| d | d | day | 86,400,000 |
| w | w | week | 604,800,000 |
| mo | mo | month | 2,592,000,000 (30d)|
| y | y | year | 31,536,000,000(365d)|
License
MIT - Scuton Technology
