@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
31 lines (29 loc) • 1.33 kB
JavaScript
import { isOptionsArgument } from "../../internal/isObject.js";
import { unixWallClock } from "../../internal/unixWallClock.js";
// src/unix/parse/parseDateFromUnix.ts
/**
* Extract the date portion from a unix epoch value.
*
* - Converts to ZonedDateTime then extracts the PlainDate.
* - Returns "" for invalid input.
*
* @param value unix epoch in milliseconds or seconds: a safe integer, or a string of optionally negative ASCII digits
* @param options optional: how `value` is read and the zone its wall clock is read in
* @returns ISO date string (e.g., "2024-03-17") or "" on invalid input
*
* @example parseDateFromUnix(1700000000000, { timeZone: "UTC" }) // "2023-11-14"
* @example parseDateFromUnix(1700000000, { epochUnit: "seconds", timeZone: "UTC" }) // "2023-11-14"
* @example parseDateFromUnix(-86400, { epochUnit: "seconds", timeZone: "UTC" }) // "1969-12-31"
* @example parseDateFromUnix("1700000000000", { timeZone: "Asia/Tokyo" }) // "2023-11-15" (digit string)
* @example parseDateFromUnix(1.5) // "" (not an integer epoch)
*/
function parseDateFromUnix(value, options) {
try {
if (!isOptionsArgument(options)) return "";
const wallClock = unixWallClock(value, options);
return wallClock === null ? "" : wallClock.toPlainDate().toString();
} catch {
return "";
}
}
export { parseDateFromUnix };