Open In App

DateTime.FromOADate() Method in C#

Last Updated : 28 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
DateTime.FromOADate(Double) Method is used to return a DateTime equivalent to the specified OLE Automation Date.
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.
Below programs illustrate the use of DateTime.FromOADate(Double) Method: Example 1: csharp
// 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);
        }
    }
}
Output:
DateTime : 3699 December 28
Example 2: For ArgumentException csharp
// 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);
        }
    }
}
Output:
The date is not a valid OLE Automation Date value.
Exception Thrown: System.ArgumentException
Reference:

Next Article

Similar Reads