JavaScript / ECMAScript

24-Hour Time in JS

Match 24-hour time formats HH:MM or HH:MM:SS with valid hour (00–23) and minute/second (00–59) ranges.

Try it in the JS tester →

Pattern

regexJS
(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d)?", "g");
const input = "14:30";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

Hour alternation enforces 00–23, minute/second classes enforce 00–59. The optional :SS group makes seconds optional.

Examples

Input

14:30

Matches

  • 14:30

Input

23:59:59

Matches

  • 23:59:59

Input

25:00

No match

Same pattern, other engines

← Back to 24-Hour Time overview (all engines)