Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join Date and Time to DateTime in VB.NET

Tags:

asp.net

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
like image 418
Mark Zukerman Avatar asked Dec 04 '22 05:12

Mark Zukerman


2 Answers

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 _
)
like image 50
Guffa Avatar answered Dec 28 '22 09:12

Guffa


DateTime exposes a method called DateTime.TimeOfDay

You can simply use DateTime.Add to append the time to the date.

Dim dateAndTime As DateTime = myDate.Add(myTime.TimeOfDay)
like image 26
Matthew Avatar answered Dec 28 '22 10:12

Matthew