formatUnix
Signature
Section titled “Signature”formatUnix(value: string | number, locale?: string, options?: FormatUnixOptions): stringimport { 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 givenepochUnit. epochUnitcontrols whether the input is interpreted as"milliseconds"or"seconds"; defaults to"milliseconds".timeZonecontrols the IANA zone used for rendering; defaults to"UTC".includeTimeZoneNameappends the localized timezone name when true.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
string | number |
unix epoch value to format (string or number, per epochUnit) |
locale |
string |
optional: BCP 47 locale tag |
Options
Section titled “Options”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 |
— |
Returns
Section titled “Returns”the formatted date/time string, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”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") // ""