Time Validation Regex: 12-Hour Format (HH:MM AM/PM)
• CronOS Team
regextimevalidationtutorial12-houram-pm
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: 12-Hour Format (HH:MM AM/PM)
Validate time in 12-hour format with AM/PM indicator, supporting optional zero-padding for hours using regex.
Pattern Breakdown
regex
^(0?[1-9]|1[0-2]):[0-5]\d\s?(AM|PM)$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
(0?[1-9]|1[0-2]) | Hour | 1-12 (optional zero-padding) |
: | Separator | Literal colon |
[0-5]\d | Minutes | 00-59 (zero-padded) |
\s? | Optional space | Optional whitespace before AM/PM |
(AM|PM) | Period | Literal AM or PM (case-sensitive) |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
(0?[1-9]|1[0-2])- Hour alternation:0?[1-9]- Hours 1-9 (optional leading zero:01-09or1-9)1[0-2]- Hours 10-12
[0-5]\d- Minutes: 00-59[0-5]- First digit 0-5\d- Second digit 0-9
\s?- Optional space before AM/PM(AM|PM)- Period indicator (case-sensitive)
Examples
Valid:
12:00 AM(midnight)12:00 PM(noon)1:30 AM09:45 PM11:59 PM12:00AM(no space, also valid)1:30AM(no space, also valid)
Invalid:
00:00 AM(hour 0 not valid in 12-hour format)13:00 PM(hour 13 not valid)12:60 AM(invalid minutes)1:5 AM(minutes not zero-padded)12:00 am(lowercase not supported)12:00(missing AM/PM)
Implementation
JavaScript
javascript
const timeRegex = /^(0?[1-9]|1[0-2]):[0-5]\d\s?(AM|PM)$/;
timeRegex.test('12:00 PM'); // true
timeRegex.test('1:30 AM'); // true
timeRegex.test('09:45 PM'); // true
timeRegex.test('13:00 PM'); // false (invalid hour)
timeRegex.test('12:60 AM'); // false (invalid minutes)
Python
python
import re
time_regex = r'^(0?[1-9]|1[0-2]):[0-5]\d\s?(AM|PM)$'
bool(re.match(time_regex, '12:00 PM')) # True
bool(re.match(time_regex, '1:30 AM')) # True
bool(re.match(time_regex, '13:00 PM')) # False (invalid hour)
Go
go
timeRegex := regexp.MustCompile(`^(0?[1-9]|1[0-2]):[0-5]\d\s?(AM|PM)$`)
timeRegex.MatchString("12:00 PM") // true
timeRegex.MatchString("1:30 AM") // true
timeRegex.MatchString("13:00 PM") // false (invalid hour)
Limitations
- Case-sensitive: Only accepts uppercase AM/PM (use
(?i)flag or[Aa][Mm]|[Pp][Mm]for case-insensitive) - No seconds: Only validates HH:MM, not HH:MM:SS
- No timezone: Doesn't include timezone information
- Format only: Validates format, not time logic
- Ambiguous noon/midnight:
12:00 AMand12:00 PMcan be confusing
When to Use
- 12-hour time input validation
- US time format
- User-friendly time input forms
- When AM/PM indicator is required
- Applications targeting US market
For production, consider:
- Case-insensitive matching:
(?i)(AM|PM)or[Aa][Mm]|[Pp][Mm] - Adding seconds support:
^(0?[1-9]|1[0-2]):[0-5]\d:[0-5]\d\s?(AM|PM)$ - Handling noon/midnight edge cases clearly
- Combining with time parsing for full validation
- Converting to 24-hour format for storage
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!