此方法用于从该实例中减去指定的时间或持续时间。此方法的重载列表中有2种方法,如下所示:
- 减(DateTime)
- 减(时间跨度)
DateTime.Subtract(DateTime)
此方法用于从该实例中减去指定的日期和时间。
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.
下面的程序说明了DateTime.Subtract(DateTime)方法的用法:
范例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
范例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)
此方法用于从该实例中减去指定的持续时间。
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.
下面的程序说明了DateTime.Subtract(TimeSpan)方法的用法:
范例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
示例2:对于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
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.subtract?view=netframework-4.7.2