Valid Date

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)
Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
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

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

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

lrbristerAuthor Commented:
Hey guys,
  I work in VB and am getting errors on code conversion tools online with your code.  It's looking for a "}" somewhere.
jabcocoConnect With a Mentor Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.