@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.
27 lines (25 loc) • 706 B
JavaScript
import { plainTimeOf } from "../../internal/inputChecks.js";
// src/plain/parse/parseSecondFromTime.ts
/**
* Return the second (0-59) for a given ISO 8601 time string.
*
* - Returns zero-padded string for second.
* - Returns "" for invalid input.
*
* @param value ISO time string
* @returns Second (00-59) or "" on invalid input
*
* @example parseSecondFromTime("12:30:45") // "45"
* @example parseSecondFromTime("12:00:00") // "00"
* @example parseSecondFromTime("invalid") // ""
*/
function parseSecondFromTime(value) {
const time = plainTimeOf(value);
if (time === null) return "";
try {
return time.second.toString().padStart(2, "0");
} catch {
return "";
}
}
export { parseSecondFromTime };