Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTime.FromOADate() Method in C#
The DateTime.FromOADate() method in C# converts an OLE Automation Date value into a DateTime object. OLE Automation Date is a floating-point representation of dates and times where the integer part represents the number of days since December 30, 1899, and the fractional part represents the time of day.
Syntax
Following is the syntax for the DateTime.FromOADate() method −
public static DateTime FromOADate(double d);
Parameters
The method takes a single parameter −
- d: A double-precision floating-point number representing the OLE Automation Date value. It can range from -657435.0 to 2958465.99999999.
Return Value
Returns a DateTime object equivalent to the OLE Automation Date value. If the value is out of range, it throws an ArgumentException.
Using DateTime.FromOADate() with Basic Values
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = DateTime.FromOADate(1.0);
DateTime d2 = DateTime.FromOADate(0.0);
DateTime d3 = DateTime.FromOADate(0.5);
Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d1);
Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d2);
Console.WriteLine("DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss}", d3);
}
}
The output of the above code is −
DateTime = 01 January 1900, 12:00:00 DateTime = 30 December 1899, 12:00:00 DateTime = 30 December 1899, 12:00:00
Using DateTime.FromOADate() with Larger Values
Example
using System;
public class Demo {
public static void Main() {
DateTime d1 = DateTime.FromOADate(375765.0);
DateTime d2 = DateTime.FromOADate(44927.75);
Console.WriteLine("Large value: {0:dd} {0:MMMM} {0:yyyy}, {0:HH}:{0:mm}:{0:ss}", d1);
Console.WriteLine("Modern date: {0:dd} {0:MMMM} {0:yyyy}, {0:HH}:{0:mm}:{0:ss}", d2);
}
}
The output of the above code is −
Large value: 21 October 2928, 00:00:00 Modern date: 01 January 2023, 18:00:00
Converting Back to OADate
Example
using System;
public class Demo {
public static void Main() {
DateTime now = new DateTime(2023, 1, 1, 15, 30, 0);
double oaDate = now.ToOADate();
DateTime converted = DateTime.FromOADate(oaDate);
Console.WriteLine("Original: {0}", now);
Console.WriteLine("OA Date: {0}", oaDate);
Console.WriteLine("Converted back: {0}", converted);
Console.WriteLine("Are equal: {0}", now == converted);
}
}
The output of the above code is −
Original: 1/1/2023 3:30:00 PM OA Date: 44927.645833333336 Converted back: 1/1/2023 3:30:00 PM Are equal: True
Conclusion
The DateTime.FromOADate() method is essential for converting OLE Automation Date values to DateTime objects. This is particularly useful when working with COM components, Excel dates, or legacy systems that use the OLE Automation Date format where dates are represented as floating-point numbers.
