此方法用於獲取新的DateTimeOffset對象,該對象將指定的毫秒數添加到當前實例的值。
用法: public DateTimeOffset AddMilliseconds (double milliseconds);
Here, it takes a number of whole and fractional milliseconds. The number can be negative or positive.
返回值:此方法返回一個對象,該對象的值是當前DateTimeOffset對象所表示的日期和時間與毫秒所表示的整數(毫秒)之和。
異常:如果所得的DateTimeOffset值小於MinValue或大於MaxValue,則此方法將提供ArgumentOutOfRangeException。
以下示例程序旨在說明DateTimeOffset.AddMilliseconds(Double)方法的用法:
示例1:
// C# program to demonstrate the
// DateTimeOffset.AddMilliseconds(Double)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTimeOffset
DateTimeOffset offset = new DateTimeOffset(2007,
6, 1, 7, 55, 0, new TimeSpan(-5, 0, 0));
// a number of whole and fractional milliseconds
// instance using AddMilliseconds() method
DateTimeOffset value = offset.AddMilliseconds(2000);
// Display the time
Console.WriteLine("DateTimeOffset is {0}", value);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
DateTimeOffset is 06/01/2007 07:55:02 -05:00
示例2:對於ArgumentOutOfRangeException
// C# program to demonstrate the
// DateTimeOffset.AddMilliseconds(Double)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTimeOffset
DateTimeOffset offset = DateTimeOffset.MaxValue;
// a number of whole and fractional milliseconds
// instance using AddMilliseconds() method
DateTimeOffset value = offset.AddMilliseconds(2000);
// Display the time
Console.WriteLine("DateTimeOffset is {0}", value);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
輸出:
Exception Thrown: System.ArgumentOutOfRangeException
參考:
相關用法
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTimeOffset.AddMilliseconds() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。