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.
Syntax: public TimeSpan Subtract (DateTime value);
Return Value: This method returns a time interval that is equal to the date and time represented by this instance minus the date and time represented by value.
Exception: This method will give ArgumentOutOfRangeException if the result is less than MinValue or greater than MaxValue.
Below programs illustrate the use of DateTime.Subtract(DateTime) Method:
Example 1:
// C# program to demonstrate the // DateTime.Subtract(DateTime) // Method using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTime
DateTime date1 = new DateTime(2011, 1,
1, 4, 0, 15);
// creating object of DateTime
DateTime date2 = new DateTime(2010, 1,
1, 4, 0, 15);
// getting ShortTime from DateTime
// using Subtract() method;
TimeSpan value = date1.Subtract(date2);
// Display the TimeSpan
Console.WriteLine( "TimeSpan between date1" +
" and date2 is {0}" , value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
} |
TimeSpan between date1 and date2 is 365.00:00:00
Example 2:
// C# program to demonstrate the // DateTime.Subtract(DateTime) // Method using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTime
DateTime date1 = DateTime.MinValue;
// creating object of DateTime
DateTime date2 = new DateTime(11119999, 1,
1, 4, 0, 15);
// getting ShortTime from DateTime
// using Subtract() method;
TimeSpan value = date1.Subtract(date2);
// Display the TimeSpan
Console.WriteLine( "TimeSpan between date1 " +
"and date2 is {0}" , value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
} |
Exception Thrown: System.ArgumentOutOfRangeException
DateTime.Subtract(TimeSpan)
This method is used to subtract the specified duration from this instance.
Syntax: public DateTime Subtract (TimeSpan value);
Return Value: This method returns an object that is equal to the date and time represented by this instance minus the time interval represented by value.
Exception: This method will give ArgumentOutOfRangeException if the result is less than MinValue or greater than MaxValue.
Below programs illustrate the use of DateTime.Subtract(TimeSpan) Method:
Example 1:
// C# program to demonstrate the // DateTime.Subtract(TimeSpan) // Method using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTime
DateTime date = new DateTime(2011, 1,
1, 4, 0, 15);
// creating object of TimeSpan
TimeSpan ts = new TimeSpan(1, 12,
15, 16);
// getting ShortTime from
// subtracting DateTime and TimeSpan
// using Subtract() method;
DateTime value = date.Subtract(ts);
// Display the TimeSpan
Console.WriteLine( "DateTime between date " +
"and ts is {0}" , value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
} |
DateTime between date and ts is 12/30/2010 15:44:59
Example 2: For ArgumentOutOfRangeException
// C# program to demonstrate the // DateTime.Subtract(TimeSpan) // Method using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTime
DateTime date = DateTime.MinValue;
// creating object of TimeSpan
TimeSpan ts = new TimeSpan(1, 12, 15, 16);
// getting ShortTime from subtracting
// DateTime and TimeSpan
// using Subtract() method;
DateTime value = date.Subtract(ts);
// Display the TimeSpan
Console.WriteLine( "DateTime between date" +
" and ts is {0}" , value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
} |
Exception Thrown: System.ArgumentOutOfRangeException
Reference: