add

Tuesday, November 1, 2011

Convert DATETIME TO TIMESPAN IN C#

In the previous post, I mentioned how to convert DATETIME to TIMESPAN. Now if the requirement is to convert DATETIME TO TIMESPAN, we can do that in one line: DateTime dt="XXX"; TimeSpan TS = new TimeSpan(dt.Now.Ticks); This will convert the DateTime to TimeSPan. Varun

4 comments:

Tom Santos said...

Extrapolating from the above suggestion, wouldn't this work:

Dim dtStartTime as new DateTime
Dim tsStartTime As TimeSpan = New TimeSpan(dtStartTime.Hour, dtStartTime.Minute, dtStartTime.Second)

Since TimeSpan takes hours, minutes, and seconds as arguments.

Web Development Company Chennai said...

Nice blog.. Thanks for sharing this blog information with us…

Anonymous said...

This does not work. Now is not a valid property on an instance of a DateTime.

Anonymous said...

Totally nonsense! This converts apples to potatoes.
TimeSpan and DateTime are there because a date is a completely different thing than a time span: What date should "one week and five hours" be???