Skip to main content
AllDevToolsHub
Back to all patterns

24-Hour Time Validation

Dates & Numbers

Validates time in 24-hour format (HH:MM or HH:MM:SS).

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

How it works

This pattern validates 24-hour time strings. Hours range from 00–23, minutes from 00–59, and the optional seconds component also ranges from 00–59. Both HH:MM and HH:MM:SS formats are accepted.

Test Cases

Should Match

  • 00:00
  • 23:59
  • 12:30:45

Should NOT Match

  • 24:00
  • 12:60
  • 1:30
  • 25:00:00

Quick Summary

Validates 24-hour time in HH:MM or HH:MM:SS format. Hours: 00–23, minutes: 00–59, seconds: 00–59 (optional). Does not handle timezone offsets, use an ISO 8601 datetime pattern for that.

Key Takeaways

Key Takeaways

  • Hours: 00–23 (zero-padded required)
  • Minutes: 00–59 (zero-padded required)
  • Seconds: 00–59 (optional, zero-padded)
  • Does not handle timezone offsets or milliseconds
Use Cases

When to use it

  • Validating time inputs in scheduling and calendar applications
  • Parsing time fields from CSV or configuration files
  • Checking cron expression time components
Watch out

Common Mistakes

  • Accepting single-digit hours like 9:30, this pattern requires zero-padding (09:30)
  • Not handling midnight as both 00:00 and 24:00, 24:00 is valid in some standards but not here
FAQ

24-Hour Time Validation, Frequently Asked

How do I validate 12-hour time (AM/PM)?

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

How do I validate a full datetime string?

Combine the ISO 8601 date pattern with this time pattern: ^\d{4}-\d{2}-\d{2}T([01]\d|2[0-3]):[0-5]\d:[0-5]\dZ$