TimeSpan.Subtract() Method in C#
Last Updated :
30 Sep, 2019
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:
using System;
class GFG {
public static void Main()
{
try {
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
using System;
class GFG {
public static void Main()
{
try {
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...