UNPKG

@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.

30 lines (28 loc) • 1.03 kB
import { Temporal } from "../../internal/temporal.js"; import { isValidDateTime } from "../validate/isValidDateTime.js"; import { isValidTime } from "../validate/isValidTime.js"; // src/plain/chop/chopSeconds.ts /** * Return an ISO string with second precision removed from a PlainDateTime or PlainTime value. * * - Truncates to minute precision. * - Accepts both PlainDateTime and PlainTime formats. * - Returns "" for invalid input. * * @param value ISO PlainDateTime or PlainTime string * @returns ISO string trimmed to minutes or "" on invalid input * * @example chopSeconds("2024-02-29T12:34:56") // "2024-02-29T12:34" * @example chopSeconds("12:34:56") // "12:34" * @example chopSeconds("invalid") // "" */ function chopSeconds(value) { try { if (isValidDateTime(value)) return Temporal.PlainDateTime.from(value).toString({ smallestUnit: "minute" }); if (isValidTime(value)) return Temporal.PlainTime.from(value).toString({ smallestUnit: "minute" }); return ""; } catch { return ""; } } export { chopSeconds };