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.
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.
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.
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.
TimeOfDay is the property of DateTime that you're looking for:
TimeSpan timeOfDay = startTime.TimeOfDay; DateTime fullStartDateTime = startDate.Add(timeOfDay);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With