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


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