Date/Time
12-Hour Time with AM/PM Regex
In the United States, Canada, Australia, India and other places, times are commonly written on a 12-hour clock with an AM or PM marker. This pattern accepts hours from 1 to 12, with or without a leading zero, two-digit minutes and an AM or PM suffix in any letter case, optionally separated by a space. It is useful for event forms, appointment booking and parsing human-written schedules. The honest limit is that it does not accept dotted forms such as a.m. or p.m., and it does not accept times without a marker.
Pattern (JavaScript)
/^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$/Starts at the start of the line/string, then a group (the character "0", optionally (zero or one time), then one of the characters 1-9 OR the character "1", then one of the characters 0-2), then the character ":", then one of the characters 0-5, then a digit, then a space, optionally (zero or one time), then one of the characters AaPp, then one of the characters Mm, then the end of the line/string.
Examples
Matches
- ✓ 9:30 AM
- ✓ 12:00pm
- ✓ 07:15 PM
Does not match
- ✗ 13:00 PM
- ✗ 9:30
- ✗ 00:30 AM
Try it
0 matches (JavaScript engine)
9:30 AM
12:00pm
07:15 PM
13:00 PM
9:30
00:30 AMCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$/g;
const text = "9:30 AM";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$')
text = '9:30 AM'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$/';
$text = '9:30 AM';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$");
String text = "9:30 AM";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$`)
text := "9:30 AM"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$");
var text = "9:30 AM";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$").unwrap();
let text = "9:30 AM";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(0?[1-9]|1[0-2]):[0-5][0-9] ?[AaPp][Mm]$/
text = '9:30 AM'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is 00:30 AM rejected?
- On a 12-hour clock, the hour after midnight is written as 12:30 AM. Hour 00 belongs to the 24-hour clock, so the pattern rejects it to avoid confusion.
- How do I accept a.m. and p.m. with dots?
- Replace [AaPp][Mm] with [AaPp]\.?[Mm]\.? so each dot is optional. This covers AM, A.M., am and a.m.
- How do I convert a match to 24-hour time?
- Capture the hour, minutes and marker, then add 12 to the hour for PM unless it is 12, and change 12 AM to 0. Doing this in code is clearer than in the regex.