Valid Date

Posted on 2009-02-16
Last Modified: 2012-05-06
In the code below is there a way to see if the selection is a valid date?
return a message if it's an invalid date or ir a field hasn't been selected.

DateTime.Parse(carPickupMonth.SelectedValue & " " & carPickupDay.SelectedValue & ", " & carPickupYear.SelectedValue)
Question by:lrbrister
    LVL 142

    Accepted Solution

    you will need error handling
    bool valid_date = false;
      DateTime dt = DateTime.Parse(carPickupMonth.SelectedValue & " " & carPickupDay.SelectedValue & ", " & carPickupYear.SelectedValue)
      valid_date = true;
    catch  {}

    Open in new window

    LVL 10

    Expert Comment

    You can also use "TryParse" instead of using a try/catch bloc.
    bool valid_date = false;
    DateTime dt;
    if (!DateTime.TryParse("", out dt))
      valid_date = false;
    //There the "dt" variable containt the datetime value 

    Open in new window

    LVL 10

    Expert Comment

    My example was in C# and you should it should be that
    if (!DateTime.TryParse(carPickupMonth.SelectedValue + " " + carPickupDay.SelectedValue + ", " + carPickupYear.SelectedValue, out dt))

    Open in new window


    Author Comment

    Hey guys,
      I work in VB and am getting errors on code conversion tools online with your code.  It's looking for a "}" somewhere.
    LVL 10

    Assisted Solution

    I'm not familiar with VB.NET but here is the MSDN reference and a simple example.

    Dim dateValue As Date
    If Date.TryParse(carPickupMonth.SelectedValue & " " & carPickupDay.SelectedValue & ", " & carPickupYear.SelectedValue, dateValue) Then
       //Do What you need
    End If

    Open in new window


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Javascript and mvc 12 36
    How to count json data 2 42 mvc C# 2 27
    WPF issue with Trigger 2 28
    With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now