Skip to main content

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.

Import

import {
formatTime,
nowTime,
nowTimeShort,
pad2,
parseTime,
secondsBetween,
} from '@rtorcato/js-common/time'

Exports

NameSummary
formatTimeFormats a Date object as HH:MM:SS.
nowTimeReturns the current time as HH:MM:SS string.
nowTimeShortReturns the current time as HH:MM string.
pad2Pads a number to two digits (e.g. 5'05').
parseTimeParses a time string (HH:MM or HH:MM:SS) into a Date object (today's date).
secondsBetweenReturns the difference in seconds between two times (Date or string).

Full reference

See the time API reference for individual function signatures.