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


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


DateTime.FromOADate(Double)方法用於返回等於指定的OLE自動化日期的DateTime。

用法: public static DateTime FromOADate (double d);
Here, it takes an OLE Automation Date value.

返回值:此方法返回一個對象,該對象表示與d相同的日期和時間。


異常:如果日期不是有效的OLE Automation Date值,則此方法將提供ArgumentException。

以下示例程序旨在說明DateTime.FromOADate(Double)方法的用法:

示例1:

// C# program to demonstrate the 
// DateTime.FromOADate(Int64) Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // converting 657435.0 OLE  
            // Automation Date value. 
            // into DateTime format 
            // using FromOADate() method 
            DateTime date2 = DateTime.FromOADate(657435.0); 
  
            // Display the date2 
            System.Console.WriteLine("DateTime "
                       + ": {0:y} {0:dd}",date2); 
                                       
        } 
  
        catch (ArgumentException e) { 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
DateTime : 3699 December 28

示例2:對於ArgumentException

// C# program to demonstrate the 
// DateTime.FromOADate(Int64) Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // converting 657435.0 OLE 
            // Automation Date value. 
            // into DateTime format 
            // using FromOADate() method 
            DateTime date2 = DateTime.FromOADate(-657435.0); 
  
            // Display the date2 
            System.Console.WriteLine("DateTime "
                      + ": {0:y} {0:dd}",date2); 
                                       
        } 
  
        catch (ArgumentException e)  
        { 
            Console.WriteLine("The date is not a valid "+ 
                           "OLE Automation Date value."); 
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
}
輸出:
The date is not a valid OLE Automation Date value.
Exception Thrown: System.ArgumentException

參考:



相關用法


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