📜  C#中的DateTime.AddTicks()方法

📅  最后修改于: 2021-05-29 14:22:22             🧑  作者: Mango

此方法用于返回新的DateTime,该日期将指定的滴答数添加到此实例的值。此方法不会更改此DateTime的值。而是返回一个新的DateTime,其值是此操作的结果。

句法:

public DateTime AddTicks (long value);

在这里,它需要100纳秒的刻度。

返回值:该方法返回一个对象,该对象的值是此实例表示的日期和时间与值表示的时间之和。

异常:如果生成的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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.addticks?view=netframework-4.7.2