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

31 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

sujitha s said...


BIG DATA Technologies provides you with a state of the art software which combines modern GPU technology (Graphic Processing Units) with the best practices in today’s Big Data platforms, providing up to 100x faster insights from data.
Bigdata Training in Chennai OMR

Shivani Yuvanjalin said...

This post is worth for me. Thank you for sharing.
ERP in Chennai
ERP Software in Chennai
SAP Business One in Chennai
SAP Hana in Chennai
SAP r3 in Chennai

john brito said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
best rpa training in bangalore
rpa training in bangalore | rpa course in bangalore
RPA training in bangalore
rpa training in chennai
rpa online training

john brito said...
This comment has been removed by the author.
saranyaregan said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
best rpa training in bangalore
rpa training in bangalore | rpa course in bangalore
RPA training in bangalore
rpa training in chennai
rpa online training

tamilsasi said...

I was recommended this web site by means of my cousin.
I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!

Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

manisha said...

Thanks for sharing your post really it was a greatly helped to my future.
apple service center chennai | Mac service center in chennai | ipod service center in chennai | Apple laptop service center in chennai

Yogayogi said...

Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in
python training Course in chennai
python training in Bangalore
Python training institute in bangalore

Yogayogi said...

I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favourites blog site list and will be checking back soon.
python training Course in chennai
python training in Bangalore
Python training institute in bangalore

noor noor said...

شركة مكافحة حشرات بالنعيرية

jefrin adams said...

Best blog thanks for sharing
Best blue prism training in chennai

sarah blossom1234567 said...

Really you have done great job,There are may person searching about that now they will find enough resources by your post
AWS Training in Bangalore

AWS Training in pune

sarah blossom1234567 said...

Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
AWS Training in Bangalore

AWS Training in pune

رجب البرنس said...


بسم الله الرحمن الرحيم نحن فى شركة الكمال نقوم بجميع عزل الخزانات المياه سواء كانت مياة الشرب او

مياه الغسيل كما اننا يوجد لدين خدمة تسليك المجارى بافضل الطرق
شركة عزل خزانات بالطائف
شركة عزل خزانات بجازان
شركة عزل خزانات بحائل
والسلامة عليكم ورحمة الله وبركاته

Ananya M said...

Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!

AWS Course | AWS Course in Chennai | AWS Training in Chennai | AWS Training