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

12 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.

Fawad Tariq said...
This comment has been removed by a blog administrator.
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???

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Web Development Company

Sathya G said...

thank you for sharing such a nice and interesting blog with us. hope it might be much useful for us. keep on updating...
SEO Company In Chennai

Iqbal Dlk said...

very nice and informative blog
big data projects chennai
mobile computing projects chennai
cloud computing projects chennai
secure computing projects chennai

1croreprojects said...

Thanks for sharing this valuable information.
dotnet projects in chennai

vinu priya said...

This was so useful and informative. The article helped me to learn something new. Android App Development Company in Chennai

Mahalyasree said...

This idea is mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing this explanation.Your final conclusion was good. We are sowing seeds and need to be patiently wait till it blossoms.
Architectural Firms in Chennai
Architects in Chennai

Abiya Carol said...

It’s really amazing that we can record what our visitors do on our site. Thanks for sharing this awesome guide. I’m happy that I came across with your site this article is on point,thanks again and have a great day. Keep update more information..

Dot Net Training in Chennai

Software Testing Training in Chennai