Back to Home

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!

Generate Regex Pattern

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

ComponentDescriptionMatches
^Start anchorEnsures match from string start
(0?[1-9]|1[0-2])Hour1-12 (optional zero-padding)
:SeparatorLiteral colon
[0-5]\dMinutes00-59 (zero-padded)
\s?Optional spaceOptional whitespace before AM/PM
(AM|PM)PeriodLiteral AM or PM (case-sensitive)
$End anchorEnsures match to string end

Detailed Breakdown

  • (0?[1-9]|1[0-2]) - Hour alternation:
    • 0?[1-9] - Hours 1-9 (optional leading zero: 01-09 or 1-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 AM
  • 09:45 PM
  • 11:59 PM
  • 12: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

  1. Case-sensitive: Only accepts uppercase AM/PM (use (?i) flag or [Aa][Mm]|[Pp][Mm] for case-insensitive)
  2. No seconds: Only validates HH:MM, not HH:MM:SS
  3. No timezone: Doesn't include timezone information
  4. Format only: Validates format, not time logic
  5. Ambiguous noon/midnight: 12:00 AM and 12:00 PM can 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!

Generate Regex Pattern