🔍Regex Recipes
Time Format (HH:MM 24-hour)
Validate 24-hour time format (HH:MM) with proper hour (00-23) and minute (00-59) validation.
Pattern
^([01]?[0-9]|2[0-3]):[0-5][0-9]$Explanation
Matches 24-hour time format. Hours: 00-23 (with optional leading zero), Minutes: 00-59.
Examples
Valid
Input
09:30
Output
✓ Match
Valid
Input
23:59
Output
✓ Match
Valid
Input
0:00
Output
✓ Match
Invalid - hour > 23
Input
24:00
Output
✗ No match
Invalid - minute > 59
Input
12:60
Output
✗ No match
Code Examples
JavaScript
const timeRegex = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/;
function parseTime(timeStr) {
if (!timeRegex.test(timeStr)) {
throw new Error('Invalid time format');
}
const [hours, minutes] = timeStr.split(':').map(Number);
return { hours, minutes };
}
const time = parseTime('14:30'); // { hours: 14, minutes: 30 }Try it Now
💡 Tips
- Normalize to always use leading zeros for consistency
- Consider adding seconds validation if needed
- Handle timezone separately if required
- Use native Date object for actual time manipulation
⚠️ Common Pitfalls
- Does not include seconds
- Leading zero is optional (accepts 9:30 or 09:30)
- No timezone information
- For user input, consider normalizing format