Tuesday, November 1, 2011

Convert TimeSpan To DateTime in c# + One line Code

We usually fall into the situation where conversion of Timespan to DateTime is required.

Here is how it can be done easily just by casting:

Suppose we have two variables DateTime and Timespan and Timespan one with some value
DateTime dt;
TimeSpan ts="XXX";

We can covnert 'ts' to 'dt' like this:

dt= Convert.ToDateTime(ts.ToString());

Thats it!!!

Hope you have liked the solution.



Anonymous said...

very nice

Anonymous said...

Hey its very short and sweet solution.

Thanks a lot

Anonymous said...

perfect, thank you very much

Glenn said...

Nice but does not work when TS includes days, e.g. TimeSpan(2, 1, 30, 10);

Doorva Solanki said...

Thanks.. Neat and Clean solution..

Albert Andrada said...

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

Albert Andrada said...

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

Kostas Greek said...

Great.. thanx

Philips Huges said...

Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much

Personal Installment Loans
Title Car loan
Cash Advance Loan

Yakov Leberman said...

After I initially commented I clicked the -Notify me when new feedback are added- checkbox and now every time a remark is added I get 4 emails with the same comment. Is there any way you possibly can remove me from that service? Thanks! mgm online casino

ganga said...

I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.Rprogramming Training in pune

manisha said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
iWatch service center chennai | apple ipad service center in chennai | apple iphone service center in chennai