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) • 991 B
import { plainDateTimeOf, plainTimeOf } from "../../internal/inputChecks.js"; // src/plain/chop/chopMilliseconds.ts /** * Return a string with millisecond precision removed from a PlainDateTime or PlainTime value. * * - Truncates to second precision. * - Accepts both PlainDateTime and PlainTime formats. * - Returns "" for invalid input. * * @param value ISO PlainDateTime or PlainTime string * @returns ISO string without milliseconds, or "" on invalid input * * @example chopMilliseconds("2024-02-29T12:34:56.789") // "2024-02-29T12:34:56" * @example chopMilliseconds("12:34:56.789") // "12:34:56" * @example chopMilliseconds("invalid") // "" */ function chopMilliseconds(value) { try { const dateTime = plainDateTimeOf(value); if (dateTime !== null) return dateTime.toString({ smallestUnit: "second" }); const time = plainTimeOf(value); if (time !== null) return time.toString({ smallestUnit: "second" }); return ""; } catch { return ""; } } export { chopMilliseconds };