C# Program to get the difference between two dates in seconds

CsharpProgrammingServer Side Programming

Set two dates.

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

Now calculate the difference between two dates.

TimeSpan ts = date2 - date1;

Move further and calculate the difference in seconds.

ts.TotalSeconds

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, 7, 15, 11, 14, 25);
      TimeSpan ts = date2 - date1;
      Console.WriteLine("No. of Seconds (Difference) = {0}", ts.TotalSeconds);
   }
}

Output

No. of Seconds (Difference) = 10745
raja
Published on 12-Sep-2018 11:57:35
Advertisements