此方法用於從該實例中減去指定的時間或持續時間。此方法的重載列表中有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#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。