C# Program to determine the difference in hours between two dates

CsharpProgrammingServer Side Programming

Set two dates.

DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);
DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);

Now, get the difference between two dates.

TimeSpan ts = date2 - date1;

Get the result i.e. the difference in hours.

ts.TotalHours

Let us see the complete code.

Example

 Live Demo

using System;
using System.Linq;
public class Demo {
   public static void Main() {
      DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);
      DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);
      TimeSpan ts = date2 - date1;
      Console.WriteLine("No. of Hours (Difference) = {0}", ts.TotalHours);
   }
}

Output

No. of Hours (Difference) = 794.984722222222
raja
Published on 11-Sep-2018 13:32:57
Advertisements