@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) • 765 B
JavaScript
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js";
// src/zoned/convert/convertZonedToUtc.ts
/**
* Convert an ISO 8601 zoned datetime string to a UTC Instant string (ISO).
*
* - Uses Temporal.ZonedDateTime.toInstant to convert.
* - Returns "" for invalid input.
*
* @param value zoned ISO 8601 datetime string
* @returns UTC Instant ISO string or "" when invalid
*
* @example convertZonedToUtc("2024-02-29T12:34:56.789+00:00[UTC]") // "2024-02-29T12:34:56.789Z"
* @example convertZonedToUtc("invalid") // ""
*/
function convertZonedToUtc(value) {
const zonedDateTime = zonedDateTimeOf(value);
if (zonedDateTime === null) return "";
try {
return zonedDateTime.toInstant().toString();
} catch {
return "";
}
}
export { convertZonedToUtc };