|
Code Sample: Find Difference Between Two DateTime Values
This code shows you how to find the difference between two DateTime values. The easiest way to do this is to use the TimeSpan class and subtract one DateTime from another.
DateTime firstDate = new DateTime(2008, 8, 1);
DateTime secondDate = DateTime.Now;
TimeSpan timeSpan = secondDate.Subtract(firstDate);
// Get Difference
Console.WriteLine("Days : " + timeSpan.Days.ToString());
Console.WriteLine("Hours : " + timeSpan.Hours.ToString());
Console.WriteLine("Minutes : " + timeSpan.Minutes.ToString());
// Get Total Hours
Console.WriteLine("Total Hours " + timeSpan.TotalHours.ToString());
Here is the output.

Note the difference between Hours and TotalHours.
Leave a Reply
3234 views, 10 so far
today |
Leave a Reply
Get Updates By Email
Popular Post
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL

