user1035217 user1035217 - 3 months ago 23
C# Question

Combine two datetime variables into one

I have a little problem that I just cannot seem to solve. I have two datetime variables, the important data in the one is the year, month and day. The other datetime variable stores the hour, minute and second.

The reason for this chaos is due to the database where I'm pulling the data from, where they have two different columns to store the actual date and the time. Here is the code:

DateTime date = Convert.ToDateTime(dTable.Rows[i][0]);
DateTime time = Convert.ToDateTime(dTable.Rows[i][1]);

DateTime newDateTime = new DateTime();

The newDateTime need to have the complete datetime set, where the date fraction is in the date variable and the time fraction is in the time variable.

If anyone can help out I will be very happy :)


This should do:

newDateTime = date.Date + time.TimeOfDay;