RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find mm/dd/yyyy hh:MM:ss AM/PM DateTime
Expression
(?n:^(?=\d)((?<month>(0?[13578])|1[02]|(0?[469]|11)(?!.31)|0?2(?(.29)(?=.29.((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))|(?!.3[01])))(?<sep>[-./])(?<day>0?[1-9]|[12]\d|3[01])\k<sep>(?<year>(1[6-9]|[2-9]\d)\d{2})(?(?=\x20\d)\x20|$))?(?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(?i:\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$)
Description
New DateTime Regex. Rebuilt better than before, better, stronger, faster.
Matches
1/31/2002 10 AM | 2/29/2004 | 4:15:04 PM
Non-Matches
2/29/2003 | 12/32/2003 | 4:00
Author Rating: The rating for this expression. Michael Ash
Source Michael Ash
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Request Small change
Name: KS
Date: 10/4/2010 4:05:04 PM
Comment:
Can you modify this to include my date time format? I have tried but with no success. Thanks, 10/04/2010 15:36:40:577


Title: Works Perfect
Name: Crystal
Date: 8/15/2010 2:37:33 PM
Comment:
Excellent. Thank you!


Title: Works Perfect
Name: Crystal
Date: 8/15/2010 2:19:20 PM
Comment:
Excellent. Thank you!


Title: Re: An issue that occured while implementing this regex
Name: Michael Ash
Date: 6/12/2006 2:02:51 PM
Comment:
Worked fine for me. What was your issue?


Title: An issue that occured while implementing this regex
Name: Rohit
Date: 5/25/2006 8:50:28 AM
Comment:
Hi Michael GREAT REGEX that you came up with. Found an issue for the date 05/31/2006 Do check it out


Title: Changing to support YYYY/MM/DD format as well
Name: Steve Nickerson
Date: 12/2/2005 10:01:34 AM
Comment:
I have a requirement to support more than one valid date format. MM/DD/YYYY and YYYY/MM/DD formats. is it possible to move these around so that the year is checked first? If so, I could append that pattern with an | (or) and it would check both, correct? If this idea will not work, what is involved in modifying to handle the other format? Thanks!


Title: Re: Need a variant of this DateTime RegEx
Name: Michael Ash
Date: 8/19/2005 10:57:57 AM
Comment:
Please see http://regexadvice.com/blogs/mash/archive/2004/06/17/309.aspx


Title: Need a variant of this DateTime RegEx
Name: Rashmi
Date: 8/18/2005 12:31:02 PM
Comment:
Hello Michael, Thank you very much for this Date / DateTime RegEx, it is simply wonderful. I'm fairly new to regular expressions, I need your help to come up with a Regular Expression which 'strictly' validates these two formats only: MM/DD/YYYY MM/DD/YYYY HH:MM:SS Would you please provide regular expressions for only these two formats? I really appreciate any help. -Regards Rashmi


Title: Re: support for milliseconds
Name: Michael Ash
Date: 6/13/2005 10:12:56 AM
Comment:
See http://www.regexlib.com/REDetails.aspx?regexp_id=1097 I added milliseconds and removed the AP/PM format. Also made all fields required.


Title: support for milliseconds
Name: arodis
Date: 6/9/2005 8:47:08 AM
Comment:
thank you Michael.Your Regex has been of great help to me. I need a small modification though.I hope you can help me on this one. I need to parse a datetime with the following format MM/dd/yyyy HH:mm:ss.fff where fff is milliseconds [000-999]. Thanks in advance


Title: Re: is it JavaScript Compatible?
Name: Michael Ash
Date: 4/20/2005 10:56:33 AM
Comment:
No, this is not javascript compatible. Please see http://regexadvice.com/blogs/mash/archive/2004/06/17/309.aspx for javascript compatible versions.


Title: is it JavaScript Compatible?
Name: Anand
Date: 4/19/2005 5:06:13 PM
Comment:
Can I use the Regular Expression in JavaScript? And what if I am validating only MM/dd/yyyy. Thanks in Advance, Anand


Title: Woohoo!
Name: Max
Date: 10/23/2004 7:35:03 PM
Comment:
Great job! I used this code and the very next day my girlfriend called me over for some somn somn! Thanks!


Title: Great code!!
Name: Anjali
Date: 9/29/2004 12:58:56 AM
Comment:
This code is really awesome!! With just a little modification it fit my requirements like a glove. Thanks Michael ;)


Title: Re: Re: Almost what I need
Name: Michael Ash
Date: 9/17/2004 4:02:32 PM
Comment:
That should be place the bar at the end of each date separator group.


Title: Re: Almost what I need
Name: Michael Ash
Date: 9/17/2004 11:05:30 AM
Comment:
Without the 4 digit year the leap year check for years ending in 00 is ambigious. You can modify http://www.regexlib.com/REDetails.aspx?regexp_id=113 to match your values by adding a veritcal bar (|) to the end of each of the date groupings. That regex however does not capture the date parts like this one.


Title: Almost what I need
Name: Ed Blackwood
Date: 9/16/2004 4:12:33 PM
Comment:
Very nice! What I need in a regex is simmilar - but I need to validate mm/dd/yy OR mmddyy - (where the delimiters are optional. I tried modifying this one, but failed miserably. Any help? (I don't need 4 digit year validation)


Title: Re: Problems in vb.net
Name: Michael Ash
Date: 8/18/2004 2:07:14 PM
Comment:
What values are you testing with? A sample of your code would also be helpful.


Title: Problems in vb.net
Name: Julie
Date: 8/18/2004 1:57:05 PM
Comment:
Hello I am trying to use this expression in a a visual studio expression validation w/VB.NET... I copied and pasted your expression from the testing page so as not to get extra CR's but it still says all dates are wrong.. any ideas? Thanks :)


Title: re: Just the date version
Name: Michael Ash
Date: 6/25/2004 4:48:15 PM
Comment:
You can't do what you are trying to do with just a regex unless you plan to rewrite it every day, that something you would have to handle in code. You can use the regex in that code to first make sure it's a valid date and if you get a match take that result and check to see if it's less than or equal to your system date. A regex itself would have no idea of what today's (or any other) date is. Regexps only matches a pattern.


Title: Just the date version
Name: John Walter
Date: 6/25/2004 1:50:10 PM
Comment:
I am a total newbie to this. What I need is a date validator only that can validate up to today's date. Future dates are not valid. Can anyone (including Michael!)post a modified version of Michael's script to achieve this?


Title: re: One small glitch?
Name: Michael Ash
Date: 6/2/2004 10:23:26 AM
Comment:
That is NOT a glich. 02/29/2000 is/was a valid date. Please refer to http://blogs.regexadvice.com/mash/archive/2004/04/23/1021.aspx


Title: One small glitch?
Name: Phil Pastor
Date: 6/2/2004 9:21:38 AM
Comment:
Michael, excellent expression! The only thing I noticed is that it allows 02/29/2000. Which is no huge deal since this invalid leap day only occurs every 400 years.


Title: Modification for just date
Name: Jason N. Gaylord
Date: 4/28/2004 9:58:44 AM
Comment:
I modified Mike's cool regex for a JS project I had. It includes just the date portion: ^(?=\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})))$


Title: re: Syntax error is expression when testing on this site
Name: Michael Ash
Date: 4/25/2004 3:01:30 AM
Comment:
please refer to http://blogs.regexadvice.com/mash/archive/2004/04/23/1021.aspx


Title: Syntax error is expression when testing on this site
Name: jendotnet
Date: 4/24/2004 10:59:43 AM
Comment:
I went to try out the expression on this site using the Javascript option. I got an alert that there is a syntax error in the regular expression. I entered "2/19/04" as my test string.


Title: re: How to access the date parts?
Name: Michael Ash
Date: 4/21/2004 1:27:08 PM
Comment:
Each of the date parts are captured groups. In VBScript groups can be reference by the SubMatches collection. In .Net its the Groups collection. The indexing between the two is a little different though. Groups are indexed first by unnamed groups in the order that they occur followed by named groups.In this expression only the named groups are captured. The day is the 3rd group. In the Submatches collection it will be objMatch.Submatch.item(2), the collection is 0 based. In the Groups collection its objMatch.Groups.item(3) because item(0) is the entire match. However using the group you can use the capture name objMatch.Groups.item("day")


Title: How to access the date parts?
Name: Matt
Date: 4/21/2004 12:22:12 PM
Comment:
You mention you can access the date parts and use it in code. I'd like to determine the day of the week for the date entered and I'm not quite sure as to how to access the portion of the date entered that represents the day. I already have the code to determine the DOW. Thanks.


Title: I'm looking into this
Name: Darren Neimke
Date: 4/15/2004 2:56:42 AM
Comment:
Michael, I'm currently looking at adding a "Copy to Clipboard" function which will copy the unbroken expression for the user.


Title: re:Problems Using in .net
Name: Michael Ash
Date: 4/15/2004 1:10:56 AM
Comment:
I have to ask a silly question. When you say you copied as is, did you copy if from this page, the same page the comments are on? The reason I ask is I just did that and pasted it directly into a sample page and it didn't work. But it was because to help this page display the regex is being wrapped after so many characters so it's adding LF\CR that aren't actually in the expression. The expression should be one long line, not 6 different lines. If you click on the test link for this regex and copy from there you won't get the extra CR's, When I removed the CR's from my sample page it worked fine.


Title: re:Problems Using in .net
Name: Hemali
Date: 4/14/2004 2:27:40 AM
Comment:
I m using vb.net as the language in the code behind..can u help out with this...i dunno y its not taking ne dates ??


Title: re:Problems Using in .net
Name: Michael Ash
Date: 4/13/2004 10:04:04 AM
Comment:
What .net language are you using? VB.net by far supports the most complete regex syntax. JScript only support a subset of the VB regex. This expression won't work with Jscript because of the syntax. Here is a JScript-safe version http://www.regexlib.com/REDetails.aspx?regexp_id=369 or http://www.regexlib.com/REDetails.aspx?regexp_id=113 but neither capture the date portions


Title: Problems Using in .net
Name: Hemali
Date: 4/13/2004 6:00:40 AM
Comment:
I have pasted this as is in the validationexpression attribute of a RegularExpressionValidator control on a webform..but it is validating none of the dates..!!!.. do u have to put ne slashes or * or nething else ...i have directly pasted this as is..is this a correct way..what modifications are required ?? (?n:^(?=\d)((?<month>(0?[13578])|1[02]|(0?[469]|11)(?!.31)|0?2(?(.29)(?=.29.((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))|(?!.3[01])))(?<sep>[-./])(?<day>0?[1-9]|[12]\d|3[01])\k<sep>(?<year>(1[6-9]|[2-9]\d)\d{2})(?(?=\x20\d)\x20))?(?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(?i:\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$)


Title: re: Its not working for dates greater than 19
Name: Michael Ash
Date: 3/18/2004 9:08:56 AM
Comment:
Works fine with the sample dates both which are greater than 19. What specfic value did you test with?


Title: Its not working for dates greater than 19
Name: Manjulata
Date: 3/18/2004 1:48:06 AM
Comment:
This expression is not working for the dates which are greater than 19 in any month.... Regards Manjulata.Y


Title: Additional help with it
Name: Danna
Date: 3/17/2004 5:09:50 PM
Comment:
This is AWESOME! I totally admire all you regex authors - no way I have the patience for it. Question - would you mind modifying for me? I have to have dates come in in the format: dd-mmm-yyyy (because of being international) ... so ... 17-Mar-2004 You would rock! Thanks.


Title: re: Works very well
Name: Michael Ash
Date: 1/21/2004 5:23:44 PM
Comment:
Thanks, glad you found it useful Just an acknowledgement


Title: Works very well
Name: Anoj
Date: 1/21/2004 3:44:52 PM
Comment:
Hello I'd like to use this in my code. Do I have to add any copy right info to use it? Like any thing other than acknowledging your name? Thank you very much for the code.


Title: Look behind version
Name: Michael Ash
Date: 12/19/2003 4:11:48 PM
Comment:
I also wrote a version of this that uses look behinds. I may post that later. Anyone who want to see it let me know.


Title: Continued
Name: Michael Ash
Date: 12/19/2003 3:51:12 PM
Comment:
hmmm part of my comment got cut off... This version while fully functional is more of a learning tool. It makes use of lookahead and named groups and named backreferences so if your engine doesn't support them please use the original versions. It should also be easier maintain since the sections are broken down This version should be faster, if that matters, than the original If you like to see just regex without the time pattern let me know. Since it checks dates just the same as this one I wasn't going to post it unless enough people ask for it


Title: Why I added this version
Name: Michael Ash
Date: 12/19/2003 2:38:17 PM
Comment:
I have posted a regex that validates datetimes before http://www.regexlib.com/REDetails.aspx?regexp_id=369 , so why am I posing this one? This is a new verion of my original datetime regex. The main difference is in the appoached used to check dates. I added the time portion just to be complete. It hasn't changed. I hope this version is easier to read for the newbies and vets alike. I've seen many variations of my original date regex http://www.regexlib.com/REDetails.aspx?regexp_id=113 . The variations I've seen of my original regex mainly involved changing the format from mm/dd to dd/mm, or less strict on the separators or the two digit year leap check. Basically the same expression just slightly modify by the user for their personal needs. Well I decide to try to rewite my own expression and use some of the new things I'd learned about regexs since writing the original. Also I wanted to take a different approach to the whole expression. This version while fully fu


Copyright © 2001-2014, RegexAdvice.com | ASP.NET Tutorials