FromOADate. Excel interop dates are stored as integers. It is possible to convert them to DateTimes. We use FromOADate to convert OLE Automation Dates to real DateTime values.
Method notes. We examine the FromOADate method. OA refers to "OLE Automation" and these are 5-digit numbers that represent a date and time.
First, FromOADate receives one double value as its parameter, and the returns a full DateTime. This will convert those 5-digit integers to the regular DateTimes.
using System;
class Program
{
static void Main()
{
var arr = new string[]
{
// OA Dates in early 2008."39456", // (1/9/2008)"39482",
"39484",
"39493",
"39496",
"39497",
"39501",
"39506",
"39510",
"39513",
"39515",
"39521", // (3/14/2008)// Different series starts here."36526", // (1/1/2000)"36557", // (2/1/2000)"39270"// (7//7/2007)
};
foreach (string b in arr)
{
// We must have a double to convert the OA date to a real date.
double d = double.Parse(b);
// Get the converted date from the OLE automation date.
DateTime conv = DateTime.FromOADate(d);
// Write to console.
Console.WriteLine("{0} = {1}",
b,
conv.ToShortDateString());
}
}
}39456 = 1/9/2008
39482 = 2/4/2008
39484 = 2/6/2008
39493 = 2/15/2008
39496 = 2/18/2008
39497 = 2/19/2008
39501 = 2/23/2008
39506 = 2/28/2008
39510 = 3/3/2008
39513 = 3/6/2008
39515 = 3/8/2008
39521 = 3/14/2008
36526 = 1/1/2000
36557 = 2/1/2000
39270 = 7/7/2007
Values must be double types. The FromOADate method requires that it receive a double value. For the custom code above, we convert the string to a double. You may not need to do this.
Summary. We used the FromOADate method on the DateTime type in the C# language. We saw the input and output. These methods are often required for legacy XLS files.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Apr 16, 2023 (edit).