user1732364
user1732364

Reputation: 985

What is a working Regex Expression for MM/DD/YYYY?

I have searched the web and found plenty of expressions for this format but yet none have worked. I used multiple test sites and all failed :( So i'm hoping i can solve this by asking for help! I simply need the regular expression for validation on a date in the format range of 01/01/20XX to 12/31/20XX

also passes this

1/1/20XX to 12/31/20XX

Once i get this expression, i'll be validating a constant string also which i'll append onto the end which will be 12:00:00 AM. Hope this mystery of such a common format gets solved soon! :)

Upvotes: 1

Views: 1191

Answers (2)

MUG4N
MUG4N

Reputation: 19717

Try this one:

^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$

if you need further explanation about the single digits see here: regex description

UPDATE:

If you wanna check for date and time use this regular expression:

^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d ([01]?[0-9]|2[0-3]):[0-5][0-9] (am|pm|AM|PM)$

I tested this one with the value "02.02.2012 12:00 AM" ... it works for me.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415705

You don't want a regex for this. Really. You want to use DateTime.TryParse() or DateTime.TryParseExact() (unless this is for javascript, in which case you really should have included that as a tag).

Upvotes: 10

Related Questions