473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex - Date

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
5 1669
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1839
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...
1
1038
by: gs | last post by:
is it OK to get regex result of pattern which has duplicate reference names like StrDatePattern As String ="\b(((?<year>\d{4})-(?<month>\d{1,2})-(?<day>\d{2}))|{(?<month>\d{1,2}) (?<day>\d{2}),...
4
3588
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
5
13810
by: shapper | last post by:
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
0
7251
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,...
1
7089
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
7517
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
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.