C# Program to Add Two TimeSpan


Firstly, set two TimeSpans −

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

To add it, use the Add() method −

TimeSpan res = t1.Add(t2);

Example

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

Updated on: 22-Jun-2020

427 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements