DateTime.Subtract() Method in C#
Last Updated :
10 Feb, 2019
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:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
DateTime date1 = new DateTime(2011, 1,
1, 4, 0, 15);
DateTime date2 = new DateTime(2010, 1,
1, 4, 0, 15);
TimeSpan value = date1.Subtract(date2);
Console.WriteLine( "TimeSpan between date1" +
" and date2 is {0}" , value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
TimeSpan between date1 and date2 is 365.00:00:00
Example 2:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
DateTime date1 = DateTime.MinValue;
DateTime date2 = new DateTime(11119999, 1,
1, 4, 0, 15);
TimeSpan value = date1.Subtract(date2);
Console.WriteLine( "TimeSpan between date1 " +
"and date2 is {0}" , value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
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:
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
DateTime date = new DateTime(2011, 1,
1, 4, 0, 15);
TimeSpan ts = new TimeSpan(1, 12,
15, 16);
DateTime value = date.Subtract(ts);
Console.WriteLine( "DateTime between date " +
"and ts is {0}" , value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
DateTime between date and ts is 12/30/2010 15:44:59
Example 2: For ArgumentOutOfRangeException
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
DateTime date = DateTime.MinValue;
TimeSpan ts = new TimeSpan(1, 12, 15, 16);
DateTime value = date.Subtract(ts);
Console.WriteLine( "DateTime between date" +
" and ts is {0}" , value);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
Exception Thrown: System.ArgumentOutOfRangeException
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...