By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,685 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,685 IT Pros & Developers. It's quick & easy.

Regex - Date

P: n/a
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

Jun 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You might want to check www.regexlib.com

"shapper" wrote:
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

Jun 8 '07 #2

P: n/a
shapper wrote:
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öran Andersson
_____
http://www.guffa.com
Jun 8 '07 #3

P: n/a
On Jun 8, 3:33 am, shapper <mdmo...@gmail.comwrote:
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

Jun 8 '07 #4

P: n/a
On Jun 8, 9:53 am, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On Jun 8, 3:33 am, shapper <mdmo...@gmail.comwrote:


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- Hide quotedtext -

- Show quoted text -
Wait, a silly typo.

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

Jun 8 '07 #5

P: n/a
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
Jun 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.