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

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.

Varun