Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two datetime variables into one (up to seconds precision)

Tags:

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.

like image 444
ceds Avatar asked Feb 15 '12 13:02

ceds


Video Answer


1 Answers

This should do:

newDateTime = date.Date + time.TimeOfDay; 
like image 174
Christian Hayter Avatar answered Sep 23 '22 17:09

Christian Hayter