C# Program to Subtract Two TimeSpan


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

Updated on: 22-Jun-2020

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements