@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) • 711 B
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDateTime } from "../validate/isValidDateTime.js";
// src/plain/chop/chopTime.ts
/**
* Return the PlainDate portion extracted from an ISO PlainDateTime string.
*
* - Extracts date components from a PlainDateTime.
* - Returns "" for invalid input.
*
* @param value ISO PlainDateTime string
* @returns ISO PlainDate string or "" on invalid input
*
* @example chopTime("2024-02-29T12:34:56") // "2024-02-29"
* @example chopTime("invalid") // ""
*/
function chopTime(value) {
if (!isValidDateTime(value)) return "";
try {
return Temporal.PlainDateTime.from(value).toPlainDate().toString();
} catch {
return "";
}
}
export { chopTime };