Regex - Date

S

shapper

Hello,

I am trying to create a RegEx which validates dates in the following
format:

dd/mm/yyyy > 27/12/2007, 02/03/2006
d/m/yy > 1/4/97, 2/3/06

Basically:
- Day can be 1 to 31 or 01, 02 ... to 31.
- Month can be 1 to 12 or 01, 02 ... to 12
- Year can from 90, 91, ... or 1990, 1991, ...

The separators could be /.-

Does anyone knows a good Regex expression for this?

And am i thinking right about this?
Should I use year always with 4 numbers?

And can I set a minimum year to be accepted? Like 1920? Only dates
after 1920?

Thanks,
Miguel
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

shapper said:
Hello,

I am trying to create a RegEx which validates dates in the following
format:

dd/mm/yyyy > 27/12/2007, 02/03/2006
d/m/yy > 1/4/97, 2/3/06

Basically:
- Day can be 1 to 31 or 01, 02 ... to 31.
- Month can be 1 to 12 or 01, 02 ... to 12
- Year can from 90, 91, ... or 1990, 1991, ...

The separators could be /.-

Does anyone knows a good Regex expression for this?

And am i thinking right about this?
Should I use year always with 4 numbers?

And can I set a minimum year to be accepted? Like 1920? Only dates
after 1920?

Thanks,
Miguel

You could also look into DateTime.TryParseExact. It will try to parse a
date in a specific format, and tells you if it was a valid date or not.
 
G

Guest

Hello,

I am trying to create a RegEx which validates dates in the following
format:

dd/mm/yyyy > 27/12/2007, 02/03/2006
d/m/yy > 1/4/97, 2/3/06

Basically:
- Day can be 1 to 31 or 01, 02 ... to 31.
- Month can be 1 to 12 or 01, 02 ... to 12
- Year can from 90, 91, ... or 1990, 1991, ...

The separators could be /.-

Does anyone knows a good Regex expression for this?

And am i thinking right about this?
Should I use year always with 4 numbers?

And can I set a minimum year to be accepted? Like 1920? Only dates
after 1920?

Thanks,
Miguel

something like this

Regex r = new Regex(@"(\d{1,2})/(\d{1,2})/(?<Year>(?:\d{4}|\d{2}))")

if(r.IsMatch(date_str))
{
string year = r.Match(date_str).Groups["Year"];
if (year.Length == 2 && Convert.ToInt32(year) >= 20) {
....date is correct
}
if (year.Length == 4 && Convert.ToInt32(year) >= 1920) {
....date is correct
}
}

I didn't test it, try it...

But I think it's better to convert it to the DateTime object, for
example with DateTime.TryParseExact as Göran has suggested
 
G

Guest

I am trying to create a RegEx which validates dates in the following
format:
dd/mm/yyyy > 27/12/2007, 02/03/2006
d/m/yy > 1/4/97, 2/3/06
Basically:
- Day can be 1 to 31 or 01, 02 ... to 31.
- Month can be 1 to 12 or 01, 02 ... to 12
- Year can from 90, 91, ... or 1990, 1991, ...
The separators could be /.-
Does anyone knows a good Regex expression for this?
And am i thinking right about this?
Should I use year always with 4 numbers?
And can I set a minimum year to be accepted? Like 1920? Only dates
after 1920?
Thanks,
Miguel

something like this

Regex r = new Regex(@"(\d{1,2})/(\d{1,2})/(?<Year>(?:\d{4}|\d{2}))")

if(r.IsMatch(date_str))
{
string year = r.Match(date_str).Groups["Year"];
if (year.Length == 2 && Convert.ToInt32(year) >= 20) {
...date is correct}

if (year.Length == 4 && Convert.ToInt32(year) >= 1920) {
...date is correct

}
}

I didn't test it, try it...

But I think it's better to convert it to the DateTime object, for
example with DateTime.TryParseExact as Göran has suggested- Hide quoted text -

- Show quoted text -

Wait, a silly typo.

(\d{1,2})/(\d{1,2})/(?<Year>\d{4}|\d{2})
 
P

Peter Bradley

Ysgrifennodd shapper:
Hello,

I am trying to create a RegEx which validates dates in the following
format:

dd/mm/yyyy > 27/12/2007, 02/03/2006
d/m/yy > 1/4/97, 2/3/06

Basically:
- Day can be 1 to 31 or 01, 02 ... to 31.
- Month can be 1 to 12 or 01, 02 ... to 12
- Year can from 90, 91, ... or 1990, 1991, ...

The separators could be /.-

Does anyone knows a good Regex expression for this?

And am i thinking right about this?
Should I use year always with 4 numbers?

And can I set a minimum year to be accepted? Like 1920? Only dates
after 1920?

Thanks,
Miguel

I'd check for a valid separator, then convert it to a DateTime and let
that check for validity using DateTime.Parse. Checking for the
separator's easy using a Regex. Something like:

(\d[/\.-]){3}

This will pick up on all the leap years and so forth.

As for setting a minimum year, you can do that when you convert the
string to a datetime - although you've made it a bit more difficult for
yourself by only using 2 digit dates in one of your formats (i.e. are
you going to allow 19? Does it mean 1919 or 2019?). So on a quick
first look I'd say you can only meaningfully do that for 4 digit dates.

HTH


Peter
 

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

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top