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

📅  最后修改于: 2021-05-29 17:44:43             🧑  作者: Mango

此方法用于将该实例的值转换为等效的OLE自动化日期。

下面的程序说明了DateTime.ToOADate()方法的用法

范例1:

// C# program to demonstrate the
// DateTime.ToOADate() 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);
  
            // Converts the value of this instance to
            // the equivalent OLE Automation date.
            // using ToOADate() method;
            double value = date.ToOADate();
  
            // Display the time
            Console.WriteLine("OLE Automation date is {0}", value);
        }
  
        catch (OverflowException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
OLE Automation date is 40544.1668402778

示例2:对于OverflowException

// C# program to demonstrate the
// DateTime.ToOADate() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date = new DateTime(0099, 1,
                                         1, 4, 0, 15);
  
            // Converts the value of this instance 
            // to the equivalent OLE Automation date.
            // using ToOADate() method;
            double value = date.ToOADate();
  
            // Display the time
            Console.WriteLine("OLE Automation date is {0}", value);
        }
  
        catch (OverflowException e)
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Exception Thrown: System.OverflowException

参考:

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