Regular expression for 12 hour time format

I need a regular expression format for checking time with AM / PM

I used this

^(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$ 

but it is not accepted at any time.

My time format will be a 12 hour format (03:23:15 AM) like this. Does anyone know a regex to work with this ???

+4
source share
4 answers

You may try?

 ([0][0-9])|([1][0-2])\:[0-5][0-9]\:[0-5][0-9][:b]*(AM|PM|am|pm) 
  • corresponds to xx: yy: zz where
  • The first term ([0][0-9])|([1][0-2]) corresponds to xx to 00-09 or 10-12
  • The following terms [0-5][0-9] compress yy, zz to minutes, seconds from 00-59 inclusive
  • Space is optional
  • Finally, AM or PM are mapped to upper or lower case
+2
source

Try the following:

 [0][0-9]|[1][0-2][:][0-5][0-9][:][0-5][0-9][ ][AM]|[PM] 
+2
source

Try the following:

 (1[012]|[1-9])(:[0-5][0-9]){2}(\\s)?(?i)(am|pm) 

The regular expression includes am pm too

 (\\s)? - follow by a white space (optional) (?i) - next checking is case insensitive (am|pm) - follow by am or pm 
+1
source

[01][0-9]:[0-5][0-9]:[0-5][0-9] (AM|PM) Corresponds to your example.

[01]\d(:[0-5]\d){2} (AM|PM) if you want to simplify it.

EDIT: As indicated in the comment, this will not work, try the following: ([01][0-2]|0?[1-9])(:[0-5][0-9]){2} (AM|PM)

+1
source

All Articles