Skip to content

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.

import { timeFormatToSeconds } from "1o1-utils";
import { timeFormatToSeconds } from "1o1-utils/time-format-to-seconds";
function timeFormatToSeconds(params: { time: string }): number
NameTypeRequiredDescription
timestringYesTime string in MM:SS or H:MM:SS / HH:MM:SS form

number — The total number of seconds.

timeFormatToSeconds({ time: "01:30" }); // 90
timeFormatToSeconds({ time: "1:01:01" }); // 3661
timeFormatToSeconds({ time: "01:01:01" }); // 3661
// Hours can be arbitrarily large
timeFormatToSeconds({ time: "100:00:00" }); // 360000
// Round-trip with secondsToTimeFormat
import { secondsToTimeFormat } from "1o1-utils";
const formatted = secondsToTimeFormat({ seconds: 7325 }); // "2:02:05"
timeFormatToSeconds({ time: formatted }); // 7325
  • Throws if time is not a string or is empty.
  • Throws if time does not match /^\d+(:\d+){1,2}$/ — e.g. "1:2:3:4", "1:abc", " 01:30" are rejected.
  • In MM:SS form, throws when minutes >= 60 (use HH:MM:SS instead).
  • In HH:MM:SS form, throws when minutes or seconds >= 60.
  • Padded and unpadded segments are both accepted: "1:2:3"3723.

parse time, hh:mm:ss to seconds, mm:ss to seconds, duration parse, time to seconds

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.