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

16 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

Amelia Lily said...

I do trust all of the concepts you’ve presented on your post. They’re really convincing and will definitely work. Still, the posts are too brief for newbies. May you please extend them a little from subsequent time?Also, I’ve shared your website in my social networks.
Best Architects in Chennai
Turnkey Interior Contractors in Chennai
Interior Contractors in Chennai
Architecture Firms in Chennai
Best Interior Designers in Chennai

Ancy merina said...

I read so many articles in your blog. All are awesome. I learnt a lot of new things with help of your blog. Thanks for sharing this wonderful information.
Best website developers in bangalore
Website designers in Bangalore
Best ECommerce Website developers in bangalore
Outsource magento ecommerce services india

Atom Steels said...

Fertility is the natural capability to produce offspring. As a measure, fertility rate is the number of offspring born per mating pair, individual or population.Human fertility depends on factors of nutrition, sexual behavior, consanguinity, culture, instinct, endocrinology, timing, economics, way of life, and emotions.Greate thinks of a fertility center for humans.

Fertility Center in OMR

Arushi Chaya said...

I have really happy to these reading your post. This product control and maintenance of our health.The daily routine can assist you weight lose quickly and safely.My life is completely reworked once I followed this diet.I feeling nice concerning myself.

Herbalife in Chennai
Herbalife Nutrition Products
Nutrition centers in Chennai
Weight Loss in Chennai
Weight Gain in Chennai