當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# DateTime.ToOADate()用法及代碼示例

此方法用於將該實例的值轉換為等效的OLE自動化日期。

用法: public double ToOADate ();

返回值:此方法返回一個雙精度浮點數,其中包含與該實例的值相等的OLE自動化日期。


異常:
OverflowException:如果此實例的值不能表示為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

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 DateTime.ToOADate() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。