473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 13798


"shapper" <md*****@gmail.comwrote in message
news:c9**********************************@c58g2000 hsc.googlegroups.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.TryParse 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.TryParse, 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.TryParseExact 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.comwrote:
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.TryParseExact 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.comwrote:
>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.TryParseExact 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
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...
1
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...
2
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...
8
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...
4
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...
11
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...
4
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...
6
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...
6
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.