Skip to content

formatUnix

formatUnix(value: string | number, locale?: string, options?: FormatUnixOptions): string
import { formatUnix } from "@northguild/gmt/unix/format";

Format a unix epoch value (string or number) as a localized date/time string.

  • Returns "" if the input is not a valid unix epoch value for the given epochUnit.
  • epochUnit controls whether the input is interpreted as "milliseconds" or "seconds"; defaults to "milliseconds".
  • timeZone controls the IANA zone used for rendering; defaults to "UTC".
  • includeTimeZoneName appends the localized timezone name when true.
Parameter Type Description
value string | number unix epoch value to format (string or number, per epochUnit)
locale string optional: BCP 47 locale tag

options

Option Type Default
epochUnit? "seconds" | "milliseconds"
timeZone? string
includeTimeZoneName? boolean
localeMatcher? "best fit" | "lookup"
weekday? "long" | "short" | "narrow"
era? "long" | "short" | "narrow"
year? "numeric" | "2-digit"
month? "long" | "short" | "narrow" | "numeric" | "2-digit"
day? "numeric" | "2-digit"
hour? "numeric" | "2-digit"
minute? "numeric" | "2-digit"
second? "numeric" | "2-digit"
timeZoneName? "long" | "short" | "shortOffset" | "longOffset" | "shortGeneric" | "longGeneric"
formatMatcher? "best fit" | "basic"
hour12? boolean
calendar? string
dayPeriod? "long" | "short" | "narrow"
numberingSystem? string
dateStyle? "long" | "short" | "medium" | "full"
timeStyle? "long" | "short" | "medium" | "full"
hourCycle? "h11" | "h12" | "h23" | "h24"
fractionalSecondDigits? 1 | 2 | 3

the formatted date/time string, or “” on invalid input

formatUnix("1710685845000", "en-US", { epochUnit: "milliseconds" }) // "3/17/2024, 2:30:45 PM"
formatUnix(1710685845000, "en-US", { epochUnit: "milliseconds" }) // "3/17/2024, 2:30:45 PM"
formatUnix("1710685845", "en-US", { epochUnit: "seconds" }) // "3/17/2024, 2:30:45 PM"
formatUnix("1710685845000", "en-US", { epochUnit: "milliseconds", includeTimeZoneName: true }) // "3/17/2024, 2:30:45 PM UTC"
formatUnix("not-a-number") // ""

unix/format/formatUnix.ts