471,350 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 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 13622


"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by J. J. Cale | last post: by
1 post views Thread by Colin Reid | last post: by
2 posts views Thread by Tim Conner | last post: by
8 posts views Thread by rjb | last post: by
4 posts views Thread by | last post: by
4 posts views Thread by ad | last post: by
6 posts views Thread by Phil Barber | last post: by
6 posts views Thread by | last post: by

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.