@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
26 lines (25 loc) • 777 B
JavaScript
import { Temporal } from "@js-temporal/polyfill";
import { isValidZonedDateTime } from "../validate/index.js";
/**
* 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") // ""
*/
export function convertZonedToUtc(value) {
if (!isValidZonedDateTime(value)) {
return "";
}
try {
return Temporal.ZonedDateTime.from(value).toInstant().toString();
}
catch {
return "";
}
}