Home  >  Article  >  Backend Development  >  C# program to add two time spans

C# program to add two time spans

WBOY
WBOYforward
2023-09-23 10:05:061113browse

添加两个时间跨度的 C# 程序

First, set up 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);
   }
}

The above is the detailed content of C# program to add two time spans. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete