Skip to content

Regex for Time (HH:MM) — Pattern Explained with Examples

DodaTech Updated 2026-06-20 2 min read

In this tutorial, you'll learn about Regex for Time (HH:MM). We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

This regex validates time strings in both 24-hour and 12-hour formats. The 24-hour pattern accepts 00:00 through 23:59, while the 12-hour pattern accepts 01:00 through 12:59 with AM/PM suffix. These patterns are essential for scheduling applications, booking systems, and log Parsing.

The Pattern

24-hour format:

/^([01]\d|2[0-3]):([0-5]\d)$/

12-hour format:

/^(0[1-9]|1[0-2]):([0-5]\d)\s?[APap][Mm]$/

Pattern Breakdown

24-hour

Part Meaning
^ Start of string
([01]\d\|2[0-3]) Hour: 00–19 or 20–23
: Literal colon separator
([0-5]\d) Minute: 00–59
$ End of string

12-hour

Part Meaning
^ Start of string
(0[1-9]\|1[0-2]) Hour: 01–12
: Literal colon separator
([0-5]\d) Minute: 00–59
\s? Optional space
[APap][Mm] AM or PM (case-insensitive)
$ End of string

Matches

  • 14:30 (24h)
  • 09:00 (24h)
  • 23:59 (24h)
  • 12:00 AM (12h)
  • 11:59 PM (12h)
  • 01:05am (12h, no space)

Does NOT Match

  • 24:00 — hour 24 is invalid
  • 12:60 — minute 60 is invalid
  • 9:00 — missing leading zero (24h)
  • 25:00 — hour exceeds 24
  • 13:00 PM — 13 is not a valid 12-hour hour
  • 00:00 AM — 00 is not valid in 12-hour format
  • 12:00 (without AM/PM) — ambiguous, 12h pattern requires AM/PM

Language Examples

JavaScript

const time24 = /^([01]\d|2[0-3]):([0-5]\d)$/;
const time12 = /^(0[1-9]|1[0-2]):([0-5]\d)\s?[APap][Mm]$/;

console.log(time24.test('14:30'));  // true
console.log(time24.test('24:00'));  // false
console.log(time12.test('12:00 AM'));  // true
console.log(time12.test('13:00 PM'));  // false

// Combine both formats
const timeRegex = /^(([01]\d|2[0-3]):([0-5]\d)|(0[1-9]|1[0-2]):([0-5]\d)\s?[APap][Mm])$/;

Python

import re

time24 = r'^([01]\d|2[0-3]):([0-5]\d)$'
time12 = r'^(0[1-9]|1[0-2]):([0-5]\d)\s?[APap][Mm]$'

print(bool(re.match(time24, '14:30')))    # True
print(bool(re.match(time24, '24:00')))    # False
print(bool(re.match(time12, '12:00 AM'))) # True
print(bool(re.match(time12, '00:00 AM'))) # False

Common Pitfalls

  1. 24:00 vs 00:0024:00 is technically valid in some standards (end of day) but most applications expect 00:00 for midnight. Choose based on your domain.

  2. AM/PM with 1212:00 AM is midnight and 12:00 PM is noon. Many users get this wrong. Consider using 24-hour format internally to avoid ambiguity.

  3. Optional seconds — Neither pattern includes seconds (:SS). If you need seconds, append (:[0-5]\d)? before the end anchor.

Real-World Use Cases

  • Appointment booking — Validating time slot inputs in scheduling systems
  • Log Parsing — Extracting timestamps from server or application logs
  • Clock widgets — Displaying and validating user-configured time formats

FAQ

{{< faq-item "How do I add seconds to the 24-hour pattern?">}}

Append (:[0-5]\d)? before the $ anchor: /^([01]\d|2[0-3]):([0-5]\d)(:[0-5]\d)?$/. This makes seconds optional. {{< /faq-item>}}

{{< faq-item "Why does 12:00 AM match but 00:00 AM does not?">}}

The 12-hour format uses hours 01–12, not 00–23. Midnight in 12-hour format is 12:00 AM, while 00:00 is a 24-hour format value. Choose the format that matches your user's expectations. {{< /faq-item>}}

  • Regex for ISO 8601 Date/Time
  • Regex for Date (YYYY-MM-DD)
  • Regex for Password Strength

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro