Mega Code Archive

 
Categories / C# Book / 02 Essential Types
 

0261 TimeSpan operators

C# overloads <,>, +,-, for TimeSpan type. The following expression evaluates to a TimeSpan of 2.5 hours: using System; using System.Text; class Sample { public static void Main() { TimeSpan ts = TimeSpan.FromHours(2) + TimeSpan.FromMinutes(30); Console.WriteLine(ts); } } The output: 02:30:00 The following expression evaluates to one second short of 10 days: using System; using System.Text; class Sample { public static void Main() { TimeSpan ts = TimeSpan.FromDays(10) - TimeSpan.FromSeconds(1); // 9.23:59:59 Console.WriteLine(ts); } } The output: 9.23:59:59