How do you convert Excel Date type to DateTime in .NET?

Posted on 2007-07-25
Last Modified: 2013-12-17
am using the Office Primary Interop Assemblies (automation) to access an
Excel file from .NET and get cell contents. I'm using C#.

When I get the cell contents that contains an Excel Date value, I am having
trouble converting from the object variable type to DateTime.
The following error is displayed:

               The specified cast is invalid.

Here's the code with error:

Range rng = (Range)oSheet.get_Range("A4", Type.Missing);
System.Object myvalue = rng.Cells.Value2; //Assign it to an object var
DateTime dt = (DateTime)myvalue;

So how do I cast the retrieved cell contents to .NET DateTime?
Question by:TALJr
    LVL 13

    Expert Comment


    Author Comment

    How does this apply to what I described?
    It looks like it solves a problem different from what I asked.
    LVL 13

    Expert Comment

    you can parse the input string from the excel file as described in the link
    LVL 29

    Expert Comment

    by:David H.H.Lee
    Dear TALJr,
    You can write your own custom format accordingly regarding the datetime that retrieved from cell content. Anyway, you can amend it based on the cell value that you retrieved.
    //myvalue.ToString() = 2007-7-26 1:00:PM <-- your cell content value for example.
    DateTime dt=Format(CDate("2007-7-26 1:00:PM"), "dd,MM yyyy hh:mm:tt")
    For more details regarding custom format date, you can check here:

    Author Comment

    The myvalue.ToString()  value is not even close to the format you have given as an example. I don't have it right now( (I'm out of my office) but it is a large integer: something like  127879.
    Would this be days after some start date? It is definitely not seconds or milliseconds.

    This is not .NET DateTIme, but Excel Date type.


    LVL 96

    Accepted Solution

    Excel uses the serial date concept, based on 1/1/1900.  The value for 1/1/1900 is 1.  Today's date is 39289.

    You need a function like this:

    public static DateTime DateSerialToDate(int value)
        DateTime dt = new DateTime(1900, 1, 1);
        return dt.AddDays(value - 2);


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now