How to validate 12-Hour Time with regex (regular Expression)
Asked Answered
B

5

7

I want to validate a 12-Hours time format like (01:05 PM) in PHP, Javascript and HTML.

I tried this:

((([1-9])|(1[0-2])):([0-5])(0|5)\s(A|P)M)

But I don't know why it doesn't work.

Bohemia answered 27/2, 2016 at 8:40 Comment(2)
@Gopal—are you sure that will match hours with a leading zero (per the OP)?Nam
Is this a JS problem, or a PHP problem? What have you tried to resolve it? Where are you stuck?Judicator
S
7

try this :

(((0[1-9])|(1[0-2])):([0-5])([0-9])\s(A|P)M)

test regex here

try this for case sensitive AM|PM|am|pm : `

(((0[1-9])|(1[0-2])):([0-5])(0|5)\s(A|P|a|p)(M|m))

`

Systaltic answered 27/2, 2016 at 8:43 Comment(6)
I mean I need AM and PM Should not be case sensitiveBohemia
@A.MO—because it needs to be 01:20 or 01:25 (the last group should be ([0-5][0-9]). ;-)Nam
(((0[1-9])|(1[0-2])):([0-5])(0|5)\s(A|P|a|p)(M|m)) : try thisSystaltic
Thanks Guys It WorkedBohemia
Note that this answer only works if the minutes are a multiple of 5, which may not be immediately obvious in the question.Constrictor
this is NOT the right answer as per comment by RobertChickpea
T
11

Try ,

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

You may test here

Tess answered 27/2, 2016 at 8:50 Comment(6)
Works like a charm <3Domingadomingo
Remove ? after 0 to require leading zero in hour.Plenish
The ^ at the beginning is very important because it prevents 17:30 PM from getting mistaken as 7:30 PM if the user inputs 24 hour time format by mistake.Crimple
/^(1[0-2]|[1-9]):[0-5][0-9] (AM|PM)$/ I just edit it a little bit and its working perfect in my case. Thanks a lot!Benevolent
proper regex is (1[0-2]|0?[0-9]):[0-5][0-9] (AM|PM) , why you use [1-9] group.. you have to use [0-9] Your answer is 100% ChatGpt responseRoz
@Roz I have given this answer in early 2016 :D where ChatGPT was a yet to be in the worldTess
S
7

try this :

(((0[1-9])|(1[0-2])):([0-5])([0-9])\s(A|P)M)

test regex here

try this for case sensitive AM|PM|am|pm : `

(((0[1-9])|(1[0-2])):([0-5])(0|5)\s(A|P|a|p)(M|m))

`

Systaltic answered 27/2, 2016 at 8:43 Comment(6)
I mean I need AM and PM Should not be case sensitiveBohemia
@A.MO—because it needs to be 01:20 or 01:25 (the last group should be ([0-5][0-9]). ;-)Nam
(((0[1-9])|(1[0-2])):([0-5])(0|5)\s(A|P|a|p)(M|m)) : try thisSystaltic
Thanks Guys It WorkedBohemia
Note that this answer only works if the minutes are a multiple of 5, which may not be immediately obvious in the question.Constrictor
this is NOT the right answer as per comment by RobertChickpea
M
2

This will work for both pm/am and PM/AM and prefixed with or without 0(Zero)

/^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/
Magazine answered 27/2, 2016 at 9:10 Comment(0)
J
1

This is correct after all tests for 12-Hrs clock format,

/(((0[1-9])|(1[0-2])):([0-5][0-9]) ?([AaPp][Mm]))/

Jammiejammin answered 18/9, 2023 at 10:44 Comment(0)
A
-1

/\b(?<!:)(?=(?:[1-9]|1[012]|0\d(?=:\d))(?::[0-5]\d\b)? ?[ap]\.?m\b/i

Alvarez answered 23/9 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.