DateTime.FromOADate(Double)方法用于返回等于指定的OLE自动化日期的DateTime。
Syntax: public static DateTime FromOADate (double d);
Here, it takes an OLE Automation Date value.
Return Value: This method returns an object that represents the same date and time as d.
Exception: This method will give ArgumentException if the date is not a valid OLE Automation Date value.
下面的程序说明了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
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.fromoadate?view=netframework-4.7.2