首先,设置两个TimeSpans-
TimeSpan t1 = TimeSpan.FromMinutes(2); TimeSpan t2 = TimeSpan.FromMinutes(1);
要添加它,请使用Subtract()
方法-
imeSpan res = t1.Subtract(t2);
这是完整的代码-
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); //减法 TimeSpan res = t1.Subtract(t2); Console.WriteLine("Resultant TimeSpan: "+res); } }
输出结果
First TimeSpan: 00:02:00 Second TimeSpan: 00:01:00 Resultant TimeSpan: 00:01:00