Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TimeSpan from DateTime

Tags:

c#

.net

Scenario:

Third party web service returns datetime in two separate fields i.e. date and time. I need a way to concatenate into single field.

e.g.   startDate='24-06-2012'  startTime='1-01-1970 1:00:00 AM'  Expected result:  fullStartDateTime='24-06-2012 1:00:00 AM' 

I tried to get the TimeSpan part from startTime and got no where. Could someone let me know if there's a smart way to achieve above.

like image 964
Nil Pun Avatar asked Jun 25 '12 06:06

Nil Pun


People also ask

How do I convert DateTime to TimeSpan?

To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property.

How do we determine a TimeSpan?

A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date.

What is TimeSpan data type?

TimeSpan (amount of time) is a new data type that can be used to store information about an elapsed time period or a time span. For example, the value in the picture (148:05:36.254) consists of 148 hours, 5 minutes, 36 seconds, and 254 milliseconds.


1 Answers

TimeOfDay is the property of DateTime that you're looking for:

TimeSpan timeOfDay = startTime.TimeOfDay; DateTime fullStartDateTime = startDate.Add(timeOfDay); 
like image 157
McGarnagle Avatar answered Oct 05 '22 21:10

McGarnagle