Red Swan
Red Swan

Reputation: 15545

regular expression for time

I want regular expression for HH:MM:SS AM/PM here HH must be 1-12 only, MM must 60 min, SS as usual (60 sec.) I any have it properly ...?

Upvotes: 7

Views: 6210

Answers (3)

mynameiscoffey
mynameiscoffey

Reputation: 15982

If you want to be able to have the zero padding as optional as well as leap seconds:

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

The breakdown:
(0?[1-9]|1[0-2]) 1-9 (with optional leading zero) or 10-12
[0-5][0-9] 00-59 (0-5 for first digit, 0-9 for second)
([0-5][0-9])|(60) Leap seconds
[AP]M AM/PM

Upvotes: 4

Mark Byers
Mark Byers

Reputation: 838106

/^([1-9]|10|11|12):[0-5][0-9]:[0-5][0-9] [AP]M$/

Upvotes: 9

jspcal
jspcal

Reputation: 51894

(0[1-9]|1[0-2]):([0-5][0-9]:[0-5][0-9]|(59|44|29):60) (AM|am|PM|pm)

Upvotes: 12

Related Questions