Skip to main content

Datetime

A minimal set of combined date+time helpers built on the native Date — ISO formatting, ISO week numbers, timezone offset, Unix timestamps.

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 { getIsoWeek, nowIso, unixTimestamp } from '@rtorcato/js-common/datetime'
import { secondsBetween } from '@rtorcato/js-common/time'

const started = new Date()
const row = {
createdAt: nowIso(), // '2026-06-12T09:04:07.123Z'
expiresAt: unixTimestamp() + 3600, // seconds, for a JWT `exp`
}

await handleRequest()
secondsBetween(started, new Date()) // elapsed seconds, floored (b - a)

getIsoWeek(new Date('2026-01-01')) // 1

Import

import { formatDateTimeLocal, getIsoWeek, getIsoWeekInfo } from '@rtorcato/js-common/datetime'

Exports

NameSummary
formatDateTimeLocalFormats a Date as YYYY-MM-DD HH:mm:ss (local time).
getIsoWeekReturns the ISO week number (1–53) of a given date using UTC.
getIsoWeekInfoReturns ISO week number and ISO week year (which may differ from calendar year).
getTimezoneOffsetReturns the timezone offset in minutes for a given date (local − UTC).
nowIsoReturns the current date and time as an ISO string (YYYY-MM-DDTHH:mm:ss.sssZ).
parseIsoDateTimeParses an ISO date-time string to a Date object.
toUtcDateReturns the UTC equivalent of a local Date.
unixMillisReturns the number of milliseconds since the Unix epoch.
unixTimestampReturns the number of seconds since the Unix epoch (UTC).

See also

  • date — calendar-day helpers — add, diff, compare, format
  • time — time-of-day parsing, HH:MM:SS formatting and secondsBetween
  • i18n — locale-aware number/date formatting and translation