Connecting Tech Pros Worldwide Forums | Help | Site Map

validating dates and times

=?Utf-8?B?cmF1bGF2aQ==?=
Guest
 
Posts: n/a
#1: Mar 11 '08
Hi:
(I know there are lots of different ways to do it) no regex, please
if we have string date yyMMdd "080230" whats the best way to test for valid
date ?
Feb will never have 30 days,

2)
if we have string HHmm "3490" whats the best way to test for valid time?
(worng hours wrong minutes)


Thanks for your ideas



Alun Harford
Guest
 
Posts: n/a
#2: Mar 12 '08

re: validating dates and times


Jon Skeet [C# MVP] wrote:
Quote:
pipo <nomail@me.comwrote:
Quote:
>1)
>DateTime test;
>try
>{
>test = DateTime.ParseExact("080230", "yyMMdd",
>CultureInfo.InvariantCulture);
>}
>catch (System.FormatException form)
>{
>//Not a valid date.
>}
>2)
>The same but then the other format.
>
Why raise an exception when TryParseExact does exactly what you need to
do, but without throwing?
I would say it depends on what you're doing.

If you expect your input to be a date, throwing an exception seems to be
perfectly reasonable - it clearly says, "Something went wrong. I'm now
doing error handling".

Alun Harford
Closed Thread