@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
25 lines (24 loc) • 793 B
JavaScript
import { Temporal } from "@js-temporal/polyfill";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
/**
* Extracts the local date portion from an ISO 8601 zoned datetime string.
*
* - Extracts date components (year, month, day) from a ZonedDateTime.
* - Returns "" for invalid input.
*
* @param value ISO 8601 zoned datetime string
* @returns Local date string YYYY-MM-DD or "" for invalid
*
* @example chopZonedTime("2024-02-29T14:30:45.123-05:00[America/New_York]") // "2024-02-29"
* @example chopZonedTime("invalid") // ""
*/
export function chopZonedTime(value) {
if (!isValidZonedDateTime(value))
return "";
try {
return Temporal.ZonedDateTime.from(value).toPlainDate().toString();
}
catch {
return "";
}
}