How to get the Time Difference in C# .net

Posted by Aamir Hasan on ASP.net Weblogs See other posts from ASP.net Weblogs or by Aamir Hasan
Published on Fri, 26 Mar 2010 07:41:00 GMT Indexed on 2010/03/26 7:53 UTC
Read the original article Hit count: 207

Filed under:
|

A DateTime instance stores both date and time information. The DateTime class can be found in the System namespace.
In order to retrieve the current system time, we can use the static property Now of the DateTime class.

In this Example i have shown, how to calculate the difference between two DateTime objects using C# syntax.

 DateTime startTime;
 DateTime endTime;

            startTime = Convert.ToDateTime("12:12 AM");

            endTime = Convert.ToDateTime("1:12 AM");

            var timeDifference = new TimeSpan(endTime.Ticks - startTime.Ticks);

Response.Write("Time difference in hours is " + timeDifference.Hours);


Link:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about datetime