DateTime.ToOADate() Method in C#

This method is used to convert the value of this instance to the equivalent OLE Automation date.

Syntax: public double ToOADate ();

Return Value: This method returns a double-precision floating-point number that contains an OLE Automation date equivalent to the value of this instance.

Exception:
OverflowException: If the value of this instance cannot be represented as an OLE Automation Date.

Below programs illustrate the use of DateTime.ToOADate() Method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

OLE Automation date is 40544.1668402778

Example 2: For OverflowException

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Exception Thrown: System.OverflowException

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.