Question

I am using OpenXML to read and excel spreadsheet and dates are stored as numbers. As I understand this is a julian format and every where I google says to use ToOADate(); to convert these numbers to dates.

The problem i am having is ToOADate(); does not resolve in my compile. I am using Visual Basic 2013, C# Asp.net MVC application.

It appears this is in the System Namespace, but I already have a using System statement.

I am obviously missing something very basic - can someone assist?

 var test = 41725;
DateTime test2 = test.ToOADate();

The compile error I am getting is

Error 4 'int' does not contain a definition for 'ToOADate' and no extension method 'ToOADate' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

Was it helpful?

Solution

If you are converting numbers to Framework DateTime instances, it is likely you are after the static method on the DateTime struct.

        DateTime dt = DateTime.FromOADate(23456);

This method needs to be scoped to DateTime. In my example, the result is 20 March 1963.

There is also an instance method on DateTime which does the reverse...

        double d = dt.ToOADate();

The result in this case is 23456.

Your code indicates that you are trying to call it as an instance method on a int, and this will not work. The compiler is telling you that there is no such method on the int primitive.

Try instead...

 var test = 41725;
DateTime test2 = DateTime.FromOADate(test);

And you'll get 27 March 2014.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top