How do you match 12 hour time hh:mm in a regex?

10,915

Solution 1

This should work:

([1-9]|1[012]):[0-5][0-9]

Solution 2

This is an example of a problem where "hey I know, I'll use regular expressions!" is the wrong solution. You can use a regular expression to check that your input format is digit-digit-colon-digit-digit, then use programming logic to ensure that the values are within the range you expect. For example:

/(\d\d?):(\d\d)/

if ($1 >= 1 && $1 <= 12 && $2 < 60) {
    // result is valid 12-hour time
}

This is much easier to read and understand than some of the obfuscated regex examples you see in other answers here.

Solution 3

^(00|0[0-9]|1[012]):[0-5][0-9] ?((a|p)m|(A|P)M)$

^ - Match the beginning of the string.

(00|0[0-9]|1[012]) - any two-digit number up to 12. Require two digits.

: - Match a colon

[0-5][0-9] - Match any two-digit number from 00 to 59.

? - Match a space zero or one times.

((a|p)m|(A|P)M) - Match am or pm, case insensitive.

$ - Match the end of the string.

Solution 4

Like this: ((?:1[0-2]|0\d)\:(?:[0-5]\d)) if you want leading 0 for the hour, ((?:1[0-2]|\d)\:(?:[0-5]\d)) if you don't and ((?:1[0-2]|0?\d)\:(?:[0-5]\d)) if you don't care.

Solution 5

why regex? you can do this will simple integer check

$str = "12:74";
list($h , $m ) = explode(":",$str);
if ( ($h <=12 && $h >=0  ) && ($m <=59 && $m >=0) ) {
    print "Time Ok.";
}else{
    print "Time not ok";
}
Share:
10,915
Adrian Sarli
Author by

Adrian Sarli

I'm a Cocoa and PHP developer.

Updated on June 30, 2022

Comments

  • Adrian Sarli
    Adrian Sarli almost 2 years

    How could you match 12 hour time in a regex-- in other words match 12:30 but not 14:74? Thanks!

  • Joe
    Joe almost 15 years
    That would mandate a leading 0 wouldn't it? e.g. would match 05:02 but not 5:02?
  • instanceof me
    instanceof me almost 15 years
    fixed that: you want, you don't or you don't care :)
  • André Eriksson
    André Eriksson almost 15 years
    Note that this one matches "3:00" in "13:00".
  • André Eriksson
    André Eriksson almost 15 years
    The above still fails for "13:00" (by matching "3:00"), and doesn't match "00:00".
  • tvanfosson
    tvanfosson almost 15 years
    ([1-9]|1[012]):[0-5][0-9] -- just for kicks.
  • Brian
    Brian almost 15 years
    @Cide: True, but Adrian did not define a delimitter. We could, of course, prepend it with [^\d].
  • ojrac
    ojrac almost 15 years
    @Brian I'd vote for \b or a lookbehind for (?<!\d) to avoid capturing anything but the time. (Semantics aside, I totally agree.) And 00:00 isn't a 12-hour time; that's written 12:00.
  • MyItchyChin
    MyItchyChin almost 15 years
    This matches parts of times over 12:59 (e.g. on 13:23 it matches 3:23)
  • Berry Langerak
    Berry Langerak almost 12 years
    Yeah, but if $str is equal to "foozah", it will break your script (as there's no index 1). Also, if $str is equal to "foo:zah", your script will say that it is a valid time, but I like to think that's not the case.
  • Rodger
    Rodger about 8 years
    Or it is an example where you are filling out something that requires regular expressions...