Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How can I convert an integer to a DateTime in C#?

I am pulling dates out of an (ancient) Betrieve database. The dates are stored in integer format (the number of days since December 31, 1899 or January 1, 1900, I believe) so recent dates are represented as integers between 38000 and 39000 or so.

As I loop through the program, I want to print these dates to an Excel file in a format that I can read (i.e. Gregorian dates). I've been unable to find a good way of converting these integer dates to Gregorian dates.

In case it would be helpful, I should mention that the date is initially retrieved as an object, and is converted to a date. If it would be easier to somehow convert the object to a DateTime, I could do that (but C# won't let me do a direct conversion).

In C++, I would retrieve it as a COleDateTime and format it into an acceptable format: myDate = tempDate.Format( "%m/%d/%Y" );

I just don't know how to do it in C#.

Help!

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    Depending on what day it really is, you might need to change baseDate. To get it to a string i think your format is

    DateTime myDate;

    myDate.ToShortDateString();

    if not here is a link for formatting dates

    http://msdn2.microsoft.com/en-us/library/k494fzbf....

    private DateTime ConvertDate(double numDays)

    {

    DateTime baseDate = new DateTime(1900, 1, 1);

    DateTime newDate = baseDate.AddDays(numDays);

    return newDate;

    }

Still have questions? Get your answers by asking now.