@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) • 812 B
JavaScript
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
// src/zoned/parse/parseDateFromZoned.ts
/**
* Extract the plain date portion from an ISO 8601 zoned datetime string.
*
* - Extracts date components from a ZonedDateTime.
* - Returns "" for invalid input.
*
* @param value zoned ISO 8601 datetime string
* @returns plain ISO date string (YYYY-MM-DD) or "" when invalid
*
* @example parseDateFromZoned("2024-02-29T12:34:56.789+00:00[UTC]") // "2024-02-29"
* @example parseDateFromZoned("invalid") // ""
*/
function parseDateFromZoned(value) {
if (!isValidZonedDateTime(value)) return "";
try {
return zonedDateTimeFrom(value)?.toPlainDate().toString();
} catch {
return "";
}
}
export { parseDateFromZoned };