timeFormatToSeconds
Parses a time-format string (MM:SS or [H]H:MM:SS) into a total number of seconds. Inverse of secondsToTimeFormat. Hours are not capped; minutes and seconds must be in 0..59.
Try it
Section titled “Try it”Import
Section titled “Import”import { timeFormatToSeconds } from "1o1-utils";import { timeFormatToSeconds } from "1o1-utils/time-format-to-seconds";Signature
Section titled “Signature”function timeFormatToSeconds(params: { time: string }): numberParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| time | string | Yes | Time string in MM:SS or H:MM:SS / HH:MM:SS form |
Returns
Section titled “Returns”number — The total number of seconds.
Examples
Section titled “Examples”timeFormatToSeconds({ time: "01:30" }); // 90timeFormatToSeconds({ time: "1:01:01" }); // 3661timeFormatToSeconds({ time: "01:01:01" }); // 3661// Hours can be arbitrarily largetimeFormatToSeconds({ time: "100:00:00" }); // 360000// Round-trip with secondsToTimeFormatimport { secondsToTimeFormat } from "1o1-utils";
const formatted = secondsToTimeFormat({ seconds: 7325 }); // "2:02:05"timeFormatToSeconds({ time: formatted }); // 7325Edge Cases
Section titled “Edge Cases”- Throws if
timeis not a string or is empty. - Throws if
timedoes not match/^\d+(:\d+){1,2}$/— e.g."1:2:3:4","1:abc"," 01:30"are rejected. - In
MM:SSform, throws when minutes >= 60 (useHH:MM:SSinstead). - In
HH:MM:SSform, throws when minutes or seconds >= 60. - Padded and unpadded segments are both accepted:
"1:2:3"→3723.
Also known as
Section titled “Also known as”parse time, hh:mm:ss to seconds, mm:ss to seconds, duration parse, time to seconds
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use timeFormatToSeconds to convert a user-entered HH:MM:SS string into seconds.