Time
A minimal set of time-of-day helpers built on the native Date. No locale-aware formatting, no durations, no timezone math.
Intentionally minimal
The date, time, and datetime modules cover everyday native-Date work and nothing more. If you need locale-aware formatting, parsing arbitrary formats, durations, calendars, or timezone math, use a dedicated library:
- date-fns — modular, tree-shakeable, immutable
- dayjs — 2 KB Moment-style API
- luxon — IANA timezones, Intl-based formatting, durations
These are stable, well-maintained, and far better than anything a thin helper set can offer.
Example
import { formatTime, nowTime, parseTime, secondsBetween } from '@rtorcato/js-common/time'
// Opening hours, stored as plain 'HH:MM:SS' strings.
const open = parseTime('09:00:00')
const close = parseTime('17:30:00')
secondsBetween(open, close) // 30600 — signed, floored (t2 - t1)
formatTime(close) // '17:30:00'
nowTime() // '14:22:07'
parseTime puts the time on today's date, so comparisons are only meaningful within one day.
Import
import { formatTime, nowTime, nowTimeShort } from '@rtorcato/js-common/time'
Exports
| Name | Summary |
|---|---|
formatTime | Formats a Date object as HH:MM:SS. |
nowTime | Returns the current time as an HH:MM:SS string. |
nowTimeShort | Returns the current time as HH:MM string. |
pad2 | Pads a number to two digits (e.g. 5 → '05'). |
parseTime | Parses a time string (HH:MM or HH:MM:SS) into a Date object (today's date). |
secondsBetween | Returns the difference in seconds between two times (as Date or string). |