DateTime.FromOADate() Method in C#

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

DateTime : 3699 December 28

Example 2: For ArgumentException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

The date is not a valid OLE Automation Date value.
Exception Thrown: System.ArgumentException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.