C# Program to Subtract Two TimeSpan

CsharpProgrammingServer Side Programming

Firstly, set two TimeSpans −

TimeSpan t1 = TimeSpan.FromMinutes(2);
TimeSpan t2 = TimeSpan.FromMinutes(1);

To add it, use the Subtract() method −

imeSpan res = t1.Subtract(t2);

Here is the complete code −

Example

 Live Demo

using System;
using System.Linq;
public class Demo {
   public static void Main() {
      TimeSpan t1 = TimeSpan.FromMinutes(2);
      TimeSpan t2 = TimeSpan.FromMinutes(1);
      Console.WriteLine("First TimeSpan: "+t1);
      Console.WriteLine("Second TimeSpan: "+t2);
      // Subtracting
      TimeSpan res = t1.Subtract(t2);
      Console.WriteLine("Resultant TimeSpan: "+res);
   }
}

Output

First TimeSpan: 00:02:00
Second TimeSpan: 00:01:00
Resultant TimeSpan: 00:01:00
raja
Published on 04-Sep-2018 16:28:45
Advertisements