此方法用于从该实例中减去指定的时间或持续时间。此方法的重载列表中有2种方法,如下所示:
- Subtract(DateTime)
- Subtract(TimeSpan)
DateTime.Subtract(DateTime)
此方法用于从该实例中减去指定的日期和时间。
用法: public TimeSpan Subtract (DateTime value);
返回值:此方法返回的时间间隔等于此实例表示的日期和时间减去值表示的日期和时间。
异常:如果结果小于MinValue或大于MaxValue,则此方法将提供ArgumentOutOfRangeException。
以下示例程序旨在说明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)
此方法用于从此实例中减去指定的持续时间。
用法: public DateTime Subtract (TimeSpan value);
返回值:此方法返回一个对象,该对象等于此实例表示的日期和时间减去value表示的时间间隔。
异常:如果结果小于MinValue或大于MaxValue,则此方法将提供ArgumentOutOfRangeException。
以下示例程序旨在说明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
参考:
相关用法
- C# Uri.IsBaseOf(Uri)用法及代码示例
- C# Random.Next()用法及代码示例
- C# Uri.ToString()用法及代码示例
- C# Uri.IsWellFormedOriginalString()用法及代码示例
- C# Uri.GetHashCode()用法及代码示例
- C# Math.Log()用法及代码示例
- C# Uri.FromHex()用法及代码示例
- C# Uri.IsHexDigit()用法及代码示例
- C# Queue.Contains()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 DateTime.Subtract() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。