Rupa
Rupa

Reputation:

Regular expression for date time format "MM/DD/YY HH:mm:ss AM/PM" in asp.net regular expression validator

Can anyone tell me the regular expression format for "MM/DD/YY HH:mm:ss AM/PM" to use it in a regular expression validator, in asp.net 2.0

Upvotes: 4

Views: 45798

Answers (7)

hex494D49
hex494D49

Reputation: 9235

Check this one. It also validates leap year.

^(((0[13578]|1[02])[\/\.-](0[1-9]|[12]\d|3[01])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((0[13456789]|1[012])[\/\.-](0[1-9]|[12]\d|30)[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/\.-](0[1-9]|1\d|2[0-8])[\/\.-]((19|[2-9]\d)\d{2})\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm))|((02)[\/\.-](29)[\/\.-]((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\s(0[0-9]|1[0-2]):(0[0-9]|[1-59]\d):(0[0-9]|[1-59]\d)\s(AM|am|PM|pm)))$

06/12/2014 12:45:56 AM // true
11-12-1965 06:04:34 PM // true
11/31/2015 11:40:00 AM // false
12-31-1945 01:38:09 PM // true
02/29/2012 09:04:10 AM // true [leap year]
02/29/2013 09:04:10 AM // false
06.12.2014 13:04:10 AM // false

Demo

Upvotes: -1

crh225
crh225

Reputation: 824

This worked for me

^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$

Source of regex

Upvotes: 3

Varun Achar
Varun Achar

Reputation: 15109

Old question but often asked.

RegEx for matching all dates, including leap years.

For DD-MM-YYYY format

^(?:31#(?:(?:0[13578])|(?:1[02]))#)|(?:(?:29|30)#(?:(?:0[1,3-9])|(?:1[0-2]))#)(?:(?:1[6-9]|[2-9]\d)\d{2})$|^(?:29#02#(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0[1-9])|(?:1\d)|(?:2[0-8]))#(?:(?:0[1-9])|(?:1[0-2]))#(?:(?:1[6-9]|[2-9]\d)\d{2})$

For MM-DD-YYYY format

^(?:(?:(?:0?[13578]|1[02])#31)\1|(?:(?:0?[1,3-9]|1[0-2])#(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2#29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))#(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

Please change # in the reg ex with your desired date separator. For a global separator replace # with (/|-|.)

RegEx tested against the following dates and running successfully. Performance is also listed.

29-02-2000 ---> true
millis 2
29-02-2006 ---> false
millis 0
29-02-2001 ---> false
millis 0
30-02-2000 ---> false
millis 0
31-11-2000 ---> false
millis 0
29-13-2000 ---> false
millis 0
31-11-2000 ---> false
millis 0
31-07-2000 ---> true
millis 0
31-08-2000 ---> true
millis 1
31-12-2000 ---> true
millis 0
30-11-2011 ---> true
millis 0
17-07-2011 ---> true
millis 0
04-10-1987 ---> true
millis 0
01-01-1900 ---> true
millis 0
24-05-88 ---> false
millis 0
24-05-88 ---> false
millis 0
29-12-2011 ---> true
millis 0
31-12-2000 ---> true
millis 0
1-1-2010 ---> false
millis 0
1-1-99 ---> false
millis 0
1-01-99 ---> false
millis 0
01-1-99 ---> false
millis 0

Upvotes: 2

Ranadheer Reddy
Ranadheer Reddy

Reputation: 4324

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

Format is MM-DD-YYYY

Upvotes: -1

Blerta
Blerta

Reputation: 2190

Can you try this

(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/\d\d (0[0-9]|1[0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9]) [AM|PM]

Hope it solves your problem.

Upvotes: -2

Vinay Sajip
Vinay Sajip

Reputation: 99355

For client-side (JavaScript) validation, you can use

^\d\d/\d\d/\d\d \d\d:\d\d:\d\d [AP]M$

which will check syntax (digits, spaces, separators), strictly, against your spec. If a match is obtained, you then need to check the ranges of the values entered (e.g. 1 <= month <= 12, etc.) in the subgroups for the returned match.

Use this site to test out the regex.

Upvotes: 1

Tim Booker
Tim Booker

Reputation: 2789

I highly recommend that you do not use regex for this. Instead, you should create your own validator (inherit BaseValidator) and use DateTime.TryParseExact to check that the value can be converted to DateTime.

Upvotes: 5

Related Questions