@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.
26 lines (24 loc) • 802 B
JavaScript
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
// src/zoned/chop/chopZonedDateTime.ts
/**
* Extracts the IANA timeZone identifier from an ISO 8601 zoned datetime string.
*
* - Extracts timezone id from a ZonedDateTime.
* - Returns "" for invalid input.
*
* @param value ISO 8601 zoned datetime string
* @returns IANA timeZone id (e.g. Europe/Paris) or "" for invalid
*
* @example chopZonedDateTime("2024-02-29T12:30:45+01:00[Europe/Paris]") // "Europe/Paris"
* @example chopZonedDateTime("invalid") // ""
*/
function chopZonedDateTime(value) {
if (!isValidZonedDateTime(value)) return "";
try {
return zonedDateTimeFrom(value).timeZoneId;
} catch {
return "";
}
}
export { chopZonedDateTime };