validating dates and times

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cmF1bGF2aQ==?=

    validating dates and times

    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

    #2
    Re: validating dates and times

    Jon Skeet [C# MVP] wrote:
    pipo <nomail@me.comw rote:
    >1)
    >DateTime test;
    >try
    >{
    >test = DateTime.ParseE xact("080230", "yyMMdd",
    >CultureInfo.In variantCulture) ;
    >}
    >catch (System.FormatE xception 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

    Comment

    Working...