473,573 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex.Validate Date and Time

Hello,

What is the Regex expression to validate a date time format as
follows:

dd-mm-yyyy hh:mm:ss

An example:
20-10-2008 10:32:45

There should be a space between date and time.

Thanks,
Miguel
Jun 27 '08 #1
5 13828


"shapper" <md*****@gmail. comwrote in message
news:c9******** *************** ***********@c58 g2000hsc.google groups.com...
Hello,

What is the Regex expression to validate a date time format as
follows:

dd-mm-yyyy hh:mm:ss

An example:
20-10-2008 10:32:45

There should be a space between date and time.

Thanks,
Miguel
\d\d-\d\d-\d{4} \d\d:\d\d:\d\d

Matches for me....although I would probably use the DateTime.TryPar se method
instead so you don't have to limit the input to a single format.

HTH,
Mythran
Jun 27 '08 #2
On Jun 19, 2:38*am, shapper <mdmo...@gmail. comwrote:
Hello,

What is the Regex expression to validate a date time format as
follows:

dd-mm-yyyy hh:mm:ss

An example:
20-10-2008 10:32:45

There should be a space between date and time.

Thanks,
Miguel
Instead of RegEx, Try using DateTime.TryPar se, that should be the
better approach.

-Cnu
Jun 27 '08 #3
shapper wrote:
Hello,

What is the Regex expression to validate a date time format as
follows:

dd-mm-yyyy hh:mm:ss

An example:
20-10-2008 10:32:45

There should be a space between date and time.

Thanks,
Miguel
If you use a regular expression, you can only validate the format of the
date, you can not validate that it's actually a valid date.

For example, a simple regular expression would allow a date like
"42-18-2008 29:63:81". A more advanced regular expression could catch
that, but it can still not tell that "29-02-2007 10:32:45" is not a
valid date.

Use the DateTime.TryPar seExact method with a format like
"dd'-'MM'-'yyyy' 'HH':'mm':'ss" to validate the string.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #4
On Jun 19, 9:47*am, Göran Andersson <gu...@guffa.co mwrote:
shapper wrote:
Hello,
What is the Regex expression to validate a date time format as
follows:
dd-mm-yyyy hh:mm:ss
An example:
20-10-2008 10:32:45
There should be a space between date and time.
Thanks,
Miguel

If you use a regular expression, you can only validate the format of the
date, you can not validate that it's actually a valid date.

For example, a simple regular expression would allow a date like
"42-18-2008 29:63:81". A more advanced regular expression could catch
that, but it can still not tell that "29-02-2007 10:32:45" is not a
valid date.

Use the DateTime.TryPar seExact method with a format like
"dd'-'MM'-'yyyy' 'HH':'mm':'ss" to validate the string.

--
Göran Andersson
_____http://www.guffa.com
Hi,

I was able to do it using:

^([0-9]{4})-([0-1][0-9])-([0-3][0-9])\s([0-1][0-9]|[2][0-3]):([0-5]
[0-9]):([0-5][0-9])$

But this does no accept empty values.

Can I create a RegEx that accepts empty values and when the value is
not empty then it validates?

Thanks,
Miguel
Jun 27 '08 #5
shapper wrote:
On Jun 19, 9:47 am, Göran Andersson <gu...@guffa.co mwrote:
>shapper wrote:
>>Hello,
What is the Regex expression to validate a date time format as
follows:
dd-mm-yyyy hh:mm:ss
An example:
20-10-2008 10:32:45
There should be a space between date and time.
Thanks,
Miguel
If you use a regular expression, you can only validate the format of the
date, you can not validate that it's actually a valid date.

For example, a simple regular expression would allow a date like
"42-18-2008 29:63:81". A more advanced regular expression could catch
that, but it can still not tell that "29-02-2007 10:32:45" is not a
valid date.

Use the DateTime.TryPar seExact method with a format like
"dd'-'MM'-'yyyy' 'HH':'mm':'ss" to validate the string.

--
Göran Andersson
_____http://www.guffa.com

Hi,

I was able to do it using:

^([0-9]{4})-([0-1][0-9])-([0-3][0-9])\s([0-1][0-9]|[2][0-3]):([0-5]
[0-9]):([0-5][0-9])$
That would accept a date like 2008-19-39...

This will get you a bit closer:

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])\s([0-1]\d|2[0-3])(:[0-5]\d){2}$

But still it has no idea about the different number of days in months. A
date like 2008-02-31 is still accepted.
But this does no accept empty values.

Can I create a RegEx that accepts empty values and when the value is
not empty then it validates?
You can just add ()? around the expression to make it conditional:

^(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])\s([0-1]\d|2[0-3])(:[0-5]\d){2})?$

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1930
by: J. J. Cale | last post by:
Little astrology program has 2 text input boxes for birth dates. There are 3 selects for day month year that will supply output to whichever text input had the focus last. If the user picks from the list boxes the relative text input field is formatted d/m/y, the format that the cgi is expecting. The user can also input directly to the text...
1
1715
by: Colin Reid | last post by:
Hey MS, here's an apparent problem in the base class library. I pulled the email validation pattern "^((*)*@(()+(*)*\.)+{2,9})" from http://regexlib.com. If I validate the email address "test@someverylongemailaddress.com" against it by just creating a RegEx and calling IsMatch it works fine, but if I create a schema defining a simple type...
2
3414
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string was splitted. I am fascinated to the powerfull use of this RegEx class, so I wonder if it could go a step further. As a question, can regex be...
8
1847
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex regxUserName = new Regex(@"(?<=User-Name = )\""(+)\""", RegexOptions.None);
4
1358
by: | last post by:
Here is an interesting one. Running asp.net 2.0 beta 2. I have a regular expression used in a regex validator that works on the client side in Firefox but not in IE. Any ideas? IE always reports the field is invalid. The expression is: ^(?!\d)(?=.*\d)(?=.*)(?=.*)(?=.*).{8,25}$ If I enter "Test_Field1" Firefox considers it valid on...
11
3090
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend a hand? import regsub
4
2790
by: ad | last post by:
I am useing VS2005 to develop wep application. I use a RegularExpress both in RegularExpressionValidator and Regex class to validate a value. The RegularExpress is 20|\-9|\-1|?\d{1} When I enter 33 and validate with RegularExpressionValidator, it fail to pass. But when I validate with regex class : Regex.IsMatch(Sight0L,...
6
4197
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character but not two or more? example myfile.doc = good My.file.doc = not good if you could give an example of the expression pattern that would most...
6
2063
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test it is called a 100 times but in my real app it could be called 50000 or more times. So my question is if it is somehow possible to speed this up...
0
7750
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7989
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8182
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8039
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5271
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3708
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3708
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2170
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1271
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.