@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.
28 lines (26 loc) • 968 B
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDateTime } from "../validate/isValidDateTime.js";
// src/plain/parse/parseMicrosecondFromDateTime.ts
/**
* Return the microsecond (0-999) for a given ISO 8601 datetime string.
*
* - Returns zero-padded string for microsecond.
* - Returns "" for invalid input.
*
* @param value ISO datetime string
* @returns Microsecond (000-999) or "" on invalid input
*
* @example parseMicrosecondFromDateTime("2024-03-15T14:30:45.123") // "000"
* @example parseMicrosecondFromDateTime("2024-03-15T14:30:45.123456") // "456"
* @example parseMicrosecondFromDateTime("2024-03-15T14:30:45.000") // "000"
* @example parseMicrosecondFromDateTime("invalid") // ""
*/
function parseMicrosecondFromDateTime(value) {
if (!isValidDateTime(value)) return "";
try {
return Temporal.PlainDateTime.from(value).microsecond.toString().padStart(3, "0");
} catch {
return "";
}
}
export { parseMicrosecondFromDateTime };