HOWTO: Check if a date is valid?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

How do I check if a date is valid (C#)? I don't want to use exception handling because there will be thousands of strings checked in a short period, many of which will not contain invalid dates, so a try/catch block around (say) System.Convert or DateTime.Parse will offer poor performance. I want to check if a string contains a valid date without using any exception handling. Any ideas

Many thanks
Richard
 
Hi Richard,

the solution is to use the Regular Expressions classes of the .NET
framework:

Here's a sample to match dates in a string:

using System.Text.RegularExpressions;

<...>

Regex regex = new Regex(
@"(?<Month>\d{1,2})/(?<Day>\d{1,2})/(?<Year>(?:\d{4}|\d{2}))",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);

Regards, Neno

Hi,

How do I check if a date is valid (C#)? I don't want to use exception
handling because there will be thousands of strings checked in a short
period, many of which will not contain invalid dates, so a try/catch block
around (say) System.Convert or DateTime.Parse will offer poor performance. I
want to check if a string contains a valid date without using any exception
handling. Any ideas?
 
How do I check if a date is valid (C#)? I don't want to use exception
handling because there will be thousands of strings checked in a short
period, many of which will not contain invalid dates, so a try/catch
block around (say) System.Convert or DateTime.Parse will offer poor
performance. I want to check if a string contains a valid date without
using any exception handling. Any ideas?

Are you *sure* that the performance using try/catch won't be good
enough? In a short test program, I just threw/caught 100,000 exceptions
in just over a second. Is that performance not good enough for you?

Do you have any idea in what *way* the dates will be invalid? If you
could get rid of the majority of "obviously" invalid ones, and then use
exceptions for the rest, that should perform well.
 
Use IsDate() function

Hi,

How do I check if a date is valid (C#)? I don't want to use exception
handling because there will be thousands of strings checked in a short
period, many of which will not contain invalid dates, so a try/catch block
around (say) System.Convert or DateTime.Parse will offer poor performance. I
want to check if a string contains a valid date without using any exception
handling. Any ideas?
 
This will not work for most International solutions, since only 2 Cultures
support this format (US-English and Swahili).

Mark Johnson, Berlin Germany
(e-mail address removed)
 
dset_Row[sa_MainFrame00Rows[14]] = Convert.ToDateTime(textBox00131.Text);

Seems to me to be the only good way of taking care of dates in C# (IsDate()
is VB).
With the exception of differences between US-Cuture and UK-Culture, it takes
care of returning a valid date.
Thankfully I use this only in checking input from Dialogs, so I have no
experince in massive type checking speed.
Considering the importance of having valid dates I would be reluctant in not
using try/catch for this.
You will be recieving an exception one way or another on recieving an
invalid date, with unexpected results if you don't use try/catch.

Mark Johnson, Berlin Germany
(e-mail address removed)

Hi,

How do I check if a date is valid (C#)? I don't want to use exception
handling because there will be thousands of strings checked in a short
period, many of which will not contain invalid dates, so a try/catch block
around (say) System.Convert or DateTime.Parse will offer poor performance. I
want to check if a string contains a valid date without using any exception
handling. Any ideas?
 
And even for those it fails to detect invalid dates - 48/51/2004 is
definitelly not a valid date yet it would pass the regular expression...

Jerry
 
There are many possible solutions to this, but they depend a lot on what
this stream of dates coming at you actually consists of. (ie how are they
being created)

If they are being created by a multiplicity of unidentified international
users typing in as text whatever they fancy then there is no solution. To
take a simple counter-example: 6/14/03 is valid in the US but not in the
UK. Without knowing the source culture you can't tell if a date is valid or
not, nor for certain what date it is if it seems to be valid.

If on the other hand you know the formatting of the incoming dates and it is
likely that some of them are wrong and you want to determine which, eg
"35/35/35" then it seems that you are posing a simple algorithmic query
about basic programming technique. ie is the month between 1 and 12, is the
day a valid day in that month including the possibility of leap years (and
understanding the difference between 1900, 2000, and 2100 etc)? Not hard, a
few lines of code and possibly quicker than a generic .net library call that
will be attempting to cover numerous cases.

Tom

Hi,

How do I check if a date is valid (C#)? I don't want to use exception
handling because there will be thousands of strings checked in a short
period, many of which will not contain invalid dates, so a try/catch block
around (say) System.Convert or DateTime.Parse will offer poor performance. I
want to check if a string contains a valid date without using any exception
handling. Any ideas?
 
Thanks everyone. It's suprising how such a simple requirement can sometimes be difficult to meet. Maybe one day C# developers will get an IsDate() function like Transact-SQL and VB programmers

I think the answer lies, for me, in exception handling. I had overestimated the "cost" of throwing exceptions, and Microsoft themselves recommend we trap in code any expected results and do not use exceptions as part of a "normal" program flow; however in this instance there doesn't seem to be an easy answer that would be flexible enough to cater for date formats from different cultures - so I guess I'll have to go against MS "good coding guidelines" and use exception handling to deal with expected results; in this one instance

Thanks again for all your contributions

Richard
 
I hope not, you cannot specify what format and culture the tested date is
in.

Jerry

Thanks everyone. It's suprising how such a simple requirement can
sometimes be difficult to meet. Maybe one day C# developers will get an
IsDate() function like Transact-SQL and VB programmers!
I think the answer lies, for me, in exception handling. I had
overestimated the "cost" of throwing exceptions, and Microsoft themselves
recommend we trap in code any expected results and do not use exceptions as
part of a "normal" program flow; however in this instance there doesn't seem
to be an easy answer that would be flexible enough to cater for date formats
from different cultures - so I guess I'll have to go against MS "good coding
guidelines" and use exception handling to deal with expected results; in
this one instance.
 
DateTime.ParseExact takes a format string, so it does not try to convert all
possible date representations.

Jerry
 
Back
Top