此方法用于返回新的DateTime,该日期将指定的滴答数添加到此实例的值。此方法不会更改此DateTime的值。而是返回一个新的DateTime,其值是此操作的结果。
用法:
public DateTime AddTicks (long value);
在这里,它需要100纳秒的刻度。
返回值:此方法返回一个对象,其值是此实例表示的日期和时间与value表示的时间之和。
异常:如果生成的DateTime小于MinValue或大于MaxValue,则此方法将提供ArgumentOutOfRangeException。
以下示例程序旨在说明DateTime.AddTicks(Int64)方法的用法:
示例1:
// C# program to demonstrate the
// DateTime.AddTicks(Double) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTime
DateTime date1 = new DateTime(2010, 1,
1, 4, 0, 15);
// adding the 3000 ticks
// using AddTicks() method;
DateTime date2 = date1.AddTicks(3000);
// Display the date1
Console.WriteLine("No. of ticks before operation: "
+ "{0}", date1.Ticks);
// Display the date2
Console.WriteLine("\nNo. of ticks after operation: "
+ "{0}", date2.Ticks);
}
catch (ArgumentOutOfRangeException e)
{
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
No. of ticks before operation: 633979152150000000 No. of ticks after operation: 633979152150003000
示例2:对于ArgumentOutOfRangeException
// C# program to demonstrate the
// DateTime.AddTicks(long) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of DateTime
// and initialize with MinValue
DateTime date1 = DateTime.MaxValue;
// Display the date1
Console.WriteLine("DateTime before operation: "
+ "{0}", date1.Ticks);
// adding the 1 Ticks
// using AddTicks() method;
DateTime date2 = date1.AddTicks(1);
// Display the date2
Console.WriteLine("\nDateTime after operation: "
+ "{0}", date2.Ticks);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("\nThe resulting DateTime is "+
"greater than the DateTime.MaxValue ");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
DateTime before operation: 3155378975999999999 The resulting DateTime is greater than the DateTime.MaxValue 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.AddTicks() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。