Nil Pun
Nil Pun

Reputation: 17383

How to get TimeSpan from DateTime

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.

Upvotes: 28

Views: 55471

Answers (2)

Ian Elvister
Ian Elvister

Reputation: 405

timespan fr = new timespan(); fr.parse(string date)

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102793

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

TimeSpan timeOfDay = startTime.TimeOfDay;
DateTime fullStartDateTime = startDate.Add(timeOfDay);

Upvotes: 66

Related Questions