Time Validation Regex: 24-Hour Format (HH:MM)
• CronOS Team
regextimevalidationtutorial24-hour
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
Time Validation Regex: 24-Hour Format (HH:MM)
Validate time in 24-hour format (HH:MM) with proper hour and minute range validation using regex.
Pattern Breakdown
regex
^([01]\d|2[0-3]):[0-5]\d$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
([01]\d|2[0-3]) | Hour | 00-23 (zero-padded) |
: | Separator | Literal colon |
[0-5]\d | Minutes | 00-59 (zero-padded) |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
([01]\d|2[0-3])- Hour alternation:[01]\d- Hours 00-19 (first digit 0 or 1, second digit 0-9)2[0-3]- Hours 20-23 (first digit 2, second digit 0-3)
[0-5]\d- Minutes: 00-59[0-5]- First digit 0-5\d- Second digit 0-9
Examples
Valid:
00:00(midnight)12:00(noon)23:59(one minute before midnight)09:3015:4501:05
Invalid:
24:00(invalid hour, should be 00:00)23:60(invalid minutes)9:30(hour not zero-padded)23:5(minutes not zero-padded)23-30(wrong separator)25:00(invalid hour)
Implementation
JavaScript
javascript
const timeRegex = /^([01]\d|2[0-3]):[0-5]\d$/;
timeRegex.test('23:59'); // true
timeRegex.test('00:00'); // true
timeRegex.test('12:30'); // true
timeRegex.test('24:00'); // false (invalid hour)
timeRegex.test('23:60'); // false (invalid minutes)
Python
python
import re
time_regex = r'^([01]\d|2[0-3]):[0-5]\d$'
bool(re.match(time_regex, '23:59')) # True
bool(re.match(time_regex, '00:00')) # True
bool(re.match(time_regex, '24:00')) # False (invalid hour)
Go
go
timeRegex := regexp.MustCompile(`^([01]\d|2[0-3]):[0-5]\d$`)
timeRegex.MatchString("23:59") // true
timeRegex.MatchString("00:00") // true
timeRegex.MatchString("24:00") // false (invalid hour)
Limitations
- No seconds: Only validates HH:MM, not HH:MM:SS
- No timezone: Doesn't include timezone information
- Format only: Validates format, not time logic
- No AM/PM: 24-hour format only
- Fixed format: Requires zero-padding
When to Use
- 24-hour time input validation
- Military time format
- International time formats
- Database time field validation
- When you need unambiguous time format
For production, consider:
- Adding seconds support:
^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$ - Timezone handling if needed
- Combining with time parsing for full validation
- Supporting optional zero-padding
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!