TimeSpan.Subtract() Method in C# Last Updated : 30 Sep, 2019 Comments Improve Suggest changes Like Article Like Report This method is used to a get new TimeSpan object whose value is the difference of the specified TimeSpan object and this instance. Syntax: public TimeSpan Subtract (TimeSpan t); Parameter: t: This parameter specifies the time interval to be subtracted. Return Value: It returns a new TimeSpan object whose value is the difference current instance and the value of t. Exception: This method will give OverflowException when the resulting TimeSpan is smaller than smallest possible value or greater than the largest possible value. Below programs illustrate the use of TimeSpan.Subtract(TimeSpan) Method: Example 1: csharp // C# program to demonstrate the // TimeSpan.Subtract(TimeSpan) Method using System; class GFG { // Main Method public static void Main() { try { // creating the TimeSpan object TimeSpan t1 = new TimeSpan(3, 22, 35, 33); TimeSpan t2 = new TimeSpan(1, 11, 15, 16); TimeSpan variable = t1.Subtract(t2); Console.WriteLine("The Timespan is : {0}", variable); } catch (OverflowException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } } Output: The Timespan is : 2.11:20:17 Example 2: For Overflow Exception csharp // C# program to demonstrate the // TimeSpan.Subtract(TimeSpan) Method using System; class GFG { // Main Method public static void Main() { try { // the TimeSpan object TimeSpan t1 = TimeSpan.MinValue; TimeSpan t2 = new TimeSpan(3, 22, 35, 33); TimeSpan variable = t1.Subtract(t2); Console.WriteLine("The Timespan is : {0}", variable); } catch (OverflowException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } } Output: Exception Thrown: System.OverflowException Reference: https://docs.microsoft.com/en-us/dotnet/api/system.timespan.subtract?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article TimeSpan.Subtract() Method in C# I IshwarGupta Follow Improve Article Tags : C# CSharp-method CSharp-TimeSpan-Struct Similar Reads TimeSpan.Add() Method in C# This method is used to a get new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance. Syntax public TimeSpan Add (TimeSpan t); Parameter: t: This parameter specifies the time interval to be added. Return Value: It returns a new TimeSpan object whose value is the 2 min read Decimal.Subtract() Method in C# This method is used to subtract the one specified Decimal value from another. Syntax: public static decimal Subtract (decimal a1, decimal a2); Parameters: a1: This parameter specifies the minuend. a2: This parameter specifies the subtrahend. Return Value: Result of subtracting a2 from a1. Exceptions 2 min read TimeSpan.Compare() Method in C# This method is used to compare two TimeSpan values and returns an integer value which indicates whether the first value is shorter than, equal to, or longer than the second value. Syntax: public static int Compare (TimeSpan t1, TimeSpan t2);Parameters: t1: Specifies the first time interval that will 2 min read DateTime.Subtract() Method in C# This method is used to subtract the specified time or duration from this instance. There are 2 methods in the overload list of this method as follows: Subtract(DateTime) Subtract(TimeSpan) DateTime.Subtract(DateTime) This method is used to subtract the specified date and time from this instance. Syn 3 min read TimeSpan.FromDays() Method in C# This method is used to get a TimeSpan that represents a specified number of days, accurate to the nearest millisecond. Syntax: public static TimeSpan FromDays (double value); Parameter: value: This parameter specifies the number of days, accurate to the nearest millisecond. Return Value: It returns 2 min read Like