using System; class MainClass { public static void Main() { TimeSpan myTimeSpan11 = TimeSpan.Parse("8:10:30"); Console.WriteLine("TimeSpan.Parse("8:10:30") = " + myTimeSpan11); TimeSpan myTimeSpan12 = TimeSpan.Parse("1.8:10:30.1234567"); Console.WriteLine("TimeSpan.Parse("1.8:10:30.1234567") = " + myTimeSpan12); } }
Author: coder
Use the Negate() method to add two TimeSpan instances
using System; class MainClass { public static void Main() { TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13); TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10); TimeSpan myTimeSpan15 = myTimeSpan13.Add(myTimeSpan14); Console.WriteLine("myTimeSpan13 = " + myTimeSpan13); Console.WriteLine("myTimeSpan14 = " + myTimeSpan14); Console.WriteLine("myTimeSpan15 = " + myTimeSpan15); Console.WriteLine("myTimeSpan15.Negate() = " + myTimeSpan15.Negate()); Console.WriteLine("myTimeSpan14.Negate() = " + myTimeSpan14.Negate()); } }
Use the Duration() method to add two TimeSpan instances
using System; class MainClass { public static void Main() { TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13); TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10); TimeSpan myTimeSpan15 = myTimeSpan13.Add(myTimeSpan14); Console.WriteLine("myTimeSpan13 = " + myTimeSpan13); Console.WriteLine("myTimeSpan14 = " + myTimeSpan14); Console.WriteLine("myTimeSpan15 = " + myTimeSpan15); Console.WriteLine("myTimeSpan15.Duration() = " + myTimeSpan15.Duration()); } }
Use the Subtract() method to subtract a TimeSpan instance from another
using System; class MainClass { public static void Main() { TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13); TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10); TimeSpan myTimeSpan15 = myTimeSpan13.Add(myTimeSpan14); Console.WriteLine("myTimeSpan13 = " + myTimeSpan13); Console.WriteLine("myTimeSpan14 = " + myTimeSpan14); Console.WriteLine("myTimeSpan15 = " + myTimeSpan15); myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14); Console.WriteLine("myTimeSpan15 = " + myTimeSpan15); } }
Use the Add() method to add a TimeSpan instance to another
using System; class MainClass { public static void Main() { TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13); TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10); TimeSpan myTimeSpan15 = myTimeSpan13.Add(myTimeSpan14); Console.WriteLine("myTimeSpan13 = " + myTimeSpan13); Console.WriteLine("myTimeSpan14 = " + myTimeSpan14); Console.WriteLine("myTimeSpan15 = " + myTimeSpan15); } }
Use the Parse() method to convert strings to TimeSpan instances
using System; class MainClass { public static void Main() { TimeSpan myTimeSpan11 = TimeSpan.Parse("8:10:30"); Console.WriteLine("TimeSpan.Parse("8:10:30") = " + myTimeSpan11); TimeSpan myTimeSpan12 = TimeSpan.Parse("1.8:10:30.1234567"); Console.WriteLine("TimeSpan.Parse("1.8:10:30.1234567") = " + myTimeSpan12); } }
Use FromDays(), FromHours(), FromMinutes(), FromSeconds(), FromMilliseconds(), and FromTicks() methods to create new TimeSpan instances
using System; class MainClass { public static void Main() { TimeSpan myTimeSpan5 = TimeSpan.FromDays(5); Console.WriteLine("TimeSpan.FromDays(5) = " + myTimeSpan5); TimeSpan myTimeSpan6 = TimeSpan.FromHours(10); Console.WriteLine("TimeSpan.FromHours(10) = " + myTimeSpan6); TimeSpan myTimeSpan7 = TimeSpan.FromMinutes(30); Console.WriteLine("TimeSpan.FromMinutes(30) = " + myTimeSpan7); TimeSpan myTimeSpan8 = TimeSpan.FromSeconds(15); Console.WriteLine("TimeSpan.FromSeconds(15) = " + myTimeSpan8); TimeSpan myTimeSpan9 = TimeSpan.FromMilliseconds(200); Console.WriteLine("TimeSpan.FromMilliseconds(200) = " + myTimeSpan9); TimeSpan myTimeSpan10 = TimeSpan.FromTicks(500); Console.WriteLine("TimeSpan.FromTicks(500) = " + myTimeSpan10); } }