Regular expression for time

I want a regular expression for HH: MM: SS AM / PM here HH should be only 1-12, MM should be 60 minutes, SS as usual (60 seconds). Do I have it right ...?

+7
time expression
source share
3 answers
(0[1-9]|1[0-2]):([0-5][0-9]:[0-5][0-9]|(59|44|29):60) (AM|am|PM|pm) 
+12
source share
 /^([1-9]|10|11|12):[0-5][0-9]:[0-5][0-9] [AP]M$/ 
+8
source share

If you want to have zero padding as optional, as well as second jumps:

 /^((0?[1-9])|(1[0-2])):[0-5]\d:(([0-5]\d)|(60)) [AP]M$/ 

Breakdown:
(0?[1-9]|1[0-2]) 1-9 (with an optional start zero) or 10-12
[0-5][0-9] 00-59 (0-5 for the first digit, 0-9 for the second)
([0-5][0-9])|(60) Jumping a second
[AP]M AM / PM

+5
source share

All Articles