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

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
^(((((0[13578])|([13578])|(1[02]))[\-\/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-\/\s]?((0[1-9])|([1-9])|([1-2][0-9])|(30)))|((02|2)[\-\/\s]?((0[1-9])|([1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s(((0[1-9])|([1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$
Description
Following expression can be used to validate a datetime column from SQL Server. This expression is an enhanced version of Scott Watermasysk's date/time submission. It now accepts leading zeros in months, days, and hours. In addition, this expression properly handles the 11th hour. Watermasysk's would take the 10th and 12th hour but not the 11th. This regex has been tweaked to do so. Does not handle the February 29th problem on non-leap years yet. Will learn a little more about RegEx and do so in later submission.
Matches
11/30/2003 10:12:24 am | 2/29/2003 08:14:56 pm | 5/22/2003
Non-Matches
11/31/2003 10:12:24 am | 2/30/2003 08:14:56 pm | 5/22/2003 14:15
Author Rating: The rating for this expression. David Darling
Source
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: Thanks, but...
Name: havax
Date: 7/17/2009 6:21:48 PM
Comment:
This works perfectly except for one thing. You only include 1,3,5,7,8,11 and 12 as valid months. if you add 2,4,6 and 9 into the beginning for both leading zeros and non leading zeros it works fine.


Title: Thanks, but...
Name: havax
Date: 7/17/2009 6:21:29 PM
Comment:
This works perfectly except for one thing. You only include 1,3,5,7,8,11 and 12 as valid months. if you add 2,4,6 and 9 into the beginning for both leading zeros and non leading zeros it works fine.


Title: Thanks
Name: Neil
Date: 9/17/2007 11:17:49 PM
Comment:
Thanks for your post, I am looking forwart this for a long time.


Title: Thanks
Name: iamtgo3
Date: 8/23/2007 12:10:10 PM
Comment:
Great regex. This is exacly what I have been looking for. Please keep me updated if you do the leap year part. This works great.


Title: Thanks
Name: Michael
Date: 1/20/2007 8:09:06 AM
Comment:
Thanks for the post


Title: hmmm
Name: Brian
Date: 12/13/2005 10:59:36 AM
Comment:
This string results in a match 01010006 is there any way to get this to require a date after say after 1900 ?


Title: Leading zeros work for ALL months using the regex given by Charley
Name: srivalli chavali
Date: 10/18/2005 6:54:13 PM
Comment:
Leading zeros work for ALL months using the regex given by Charley [ Title: Oops, this is the one that works Name: Charley Date: 9/2/2003 2:45:06 PM Comment: This one fixes the leading zeros and also the 11th hour problems with Scott's regex. ^(((((0?[13578])|(1[0-2]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[ 1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s(((0?[1-9])|(1[0- 2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$ ]


Title: Good one but doesn't match leading zeros in some months
Name: srivalli chavali
Date: 10/18/2005 6:39:01 PM
Comment:
This is very helpful. But it does not match 09/09/2005. That is, it does not match leading zeros in some months(04,06,09) I think it should be fixed.


Title: Works great in C# Regular Expression
Name: Joe Krauss
Date: 5/28/2005 9:58:14 PM
Comment:
Thanks David for taking the time to build this expression and post it. The expression works perfect in the C# application I'm working on. Thanks again!


Title: Oops, this is the one that works
Name: Charley
Date: 9/2/2003 2:45:06 PM
Comment:
This one fixes the leading zeros and also the 11th hour problems with Scott's regex. ^(((((0?[13578])|(1[0-2]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$


Title: This doesn't work for leading zeros
Name: Charley
Date: 9/2/2003 2:40:27 PM
Comment:
Here is a regex for SQL Server dates that will account for leading zeros on the month, day and hour. ^(((((0?[13578])|(1[0-2]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s(((0?[1-9])|(1[02]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$


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