asp.netvb.net

Join Date and Time to DateTime in VB.NET


I am retrieving data from DB where there is a separate date and time fields. I want to join them into a DateTime field in my VB.NET project. How would you suggest accomplishing this?

I tried this but it's not working for me. "String was not recognized as a valid DateTime."

Dim InDate As New DateTime(incident.IncidentDate.Value.Year, incident.IncidentDate.Value.Month, incident.IncidentDate.Value.Day, 0, 0, 0)
                    Dim InTime As New DateTime(1900, 1, 1, incident.IncidentTime.Value.Hour, incident.IncidentTime.Value.Minute, incident.IncidentTime.Value.Second)


                    Dim combinedDateTime As DateTime = DateTime.Parse((InDate.ToString("dd/MM/yyyy") + " " + InTime.ToString("hh:mm tt")))

                    rdtpIncidentDateTime.SelectedDate = combinedDateTime

Solution

  • You can just create a new DateTime value from the components in InDate and InTime:

    Dim combinedDateTime As DateTime = New DateTime( _
      InDate.Year, InDate.Month, InDate.Day, _
      InTime.Hour, InTime.Minute, InTime.Second _
    )