hh-mm-ss Time Formatting Utility

1.2.0 · maintenance · verified Tue Apr 21

The `hh-mm-ss` package provides a lightweight and focused utility for converting time representations between numerical values (seconds or milliseconds) and formatted strings like `hh:mm:ss`. Currently stable at version `1.2.0`, the library appears to have a conservative release cadence, with updates primarily focused on adding new format support and convenience methods incrementally, rather than frequent major changes. Its core differentiator lies in its specialization, offering a simpler alternative to larger, more comprehensive date-time libraries by focusing solely on common `hh:mm:ss` patterns and handling ambiguities with optional format parameters. It supports various time string patterns, including `mm:ss`, `hh:mm`, `hh:mm:ss`, and their millisecond-inclusive variants like `mm:ss.sss`, with a mechanism to automatically extend output formats to preserve precision when milliseconds are present.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates converting various time string formats to seconds and milliseconds, and vice-versa, using explicit format parameters to avoid ambiguity.

import TimeFormat from 'hh-mm-ss';

// Convert formatted strings to seconds
console.log('137:00:00 to seconds:', TimeFormat.toS('137:00:00'));            // Expected: 493200
console.log('02:00 (mm:ss default) to seconds:', TimeFormat.toS('02:00'));    // Expected: 120 (2 minutes)
console.log('02:00 (hh:mm specified) to seconds:', TimeFormat.toS('02:00', 'hh:mm')); // Expected: 7200 (2 hours)

// Convert seconds to formatted strings
console.log('194 seconds to mm:ss:', TimeFormat.fromS(194));                 // Expected: '03:14'
console.log('150 seconds to hh:mm:ss:', TimeFormat.fromS(150, 'hh:mm:ss'));  // Expected: '00:02:30'
console.log('8100 seconds to hh:mm:', TimeFormat.fromS(8100, 'hh:mm'));      // Expected: '02:15'

// Convert milliseconds to formatted strings
console.log('12345 ms to mm:ss.sss:', TimeFormat.fromMs(12345));             // Expected: '00:12.345'

view raw JSON →