DEV Community

ClipA Video
ClipA Video

Posted on

Parsing and Validating Video Timestamps in JavaScript

Any tool that cuts a clip out of a longer video starts with the same small problem: people type timestamps in many shapes. 1:05, 01:05, 65, 1:02:03 and 1m5s all turn up in real forms. If you store the raw text, every later step (preview, trimming, the download name) has to guess. Parse once, validate once, and work in seconds everywhere else.

One parser for the common formats

function toSeconds(input) {
  const s = String(input).trim().toLowerCase();
  if (/^\d+(\.\d+)?$/.test(s)) return Number(s);          // "65" or "65.5"
  const hms = s.match(/^(?:(\d+)h)?\s*(?:(\d+)m)?\s*(?:(\d+(?:\.\d+)?)s)?$/);
  if (hms && s) {                                            // "1m5s", "2h"
    const [, h = 0, m = 0, sec = 0] = hms;
    return Number(h) * 3600 + Number(m) * 60 + Number(sec);
  }
  const parts = s.split(':');
  if (parts.length < 2 || parts.length > 3) return NaN;
  if (!parts.every(p => /^\d+(\.\d+)?$/.test(p))) return NaN;
  return parts.reduce((total, p) => total * 60 + Number(p), 0);
}
Enter fullscreen mode Exit fullscreen mode

The colon branch uses a simple fold: each part multiplies the running total by 60, so 1:02:03 becomes ((1 * 60) + 2) * 60 + 3 = 3723.

Validate the range, not just the numbers

Two valid timestamps can still make a useless clip. Check them together against the video length:

function checkRange(startText, endText, duration, maxLength = 600) {
  const start = toSeconds(startText);
  const end = toSeconds(endText);
  if (Number.isNaN(start) || Number.isNaN(end)) return { ok: false, error: 'Use mm:ss or hh:mm:ss' };
  if (start >= end) return { ok: false, error: 'End must be after start' };
  if (end > duration) return { ok: false, error: `The video is only ${format(duration)} long` };
  if (end - start > maxLength) return { ok: false, error: 'Clip is longer than the limit' };
  return { ok: true, start, end };
}
Enter fullscreen mode Exit fullscreen mode

Show the error next to the field that caused it. "End must be after start" is the one people hit most, usually because they swapped the fields.

Format it back for people

function format(sec) {
  sec = Math.round(sec);
  const h = Math.floor(sec / 3600);
  const m = Math.floor((sec % 3600) / 60);
  const s = String(sec % 60).padStart(2, '0');
  return h ? `${h}:${String(m).padStart(2, '0')}:${s}` : `${m}:${s}`;
}
Enter fullscreen mode Exit fullscreen mode

Echo the normalised value back into the input on blur. Someone who typed 65 sees 1:05, which confirms you understood them.

Small details that save support emails

  • Trim spaces and accept a comma as a decimal separator if your users are in Europe.
  • Round to whole seconds unless your trimmer really cuts on frames; showing 1:05.437 invites questions.
  • Pre-fill the end field with start + 30 seconds. Most clips people make are short.
  • Accept a pasted YouTube link with ?t=95 and use it as the start time.

The last point matters more than it looks. A real-world example is ClipAVideo's YouTube trimmer, where you paste a link, set start and end, and preview before downloading. Its whole flow depends on getting these two numbers right, and the parsing above is roughly what any such form needs.

Write a handful of unit tests for toSeconds (empty string, 1:60, -5, 1:02:03) and you will rarely think about timestamps again.

Top comments (0)