@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.
28 lines (26 loc) • 985 B
JavaScript
import { hasSecond60 } from "../../plain/validate/hasSecond60.js";
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
// src/zoned/parse/parseDateTimeFromZoned.ts
/**
* Parse a zoned datetime and return the plain local datetime portion as an ISO plain datetime string.
*
* - Uses Temporal.ZonedDateTime.from to parse.
* - Rejects leap seconds.
* - Returns "" for invalid input.
*
* @param value zoned ISO 8601 datetime string
* @returns plain local datetime string or "" when invalid
*
* @example parseDateTimeFromZoned("2024-02-29T12:34:56.789+00:00[UTC]") // "2024-02-29T12:34:56.789"
* @example parseDateTimeFromZoned("invalid") // ""
*/
const parseDateTimeFromZoned = (value) => {
if (!isValidZonedDateTime(value) || hasSecond60(value)) return "";
try {
return zonedDateTimeFrom(value).toPlainDateTime().toString();
} catch {
return "";
}
};
export { parseDateTimeFromZoned };