Regex.Validate Date and Time

S

shapper

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
 
M

Mythran

shapper said:
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
 
D

Duggi

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
 
G

Göran Andersson

shapper said:
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.
 
S

shapper

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.

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
 
G

Göran Andersson

shapper said:
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.

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})?$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top