当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。