Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Double to DateTime?

I have a .CSV file which I am reading into a C# program. In one of the columns, there is a date, but it is in the "general" format, so it shows up in the .CSV as a number. For example: 41172.

How can I convert this number to a date with format dd/mm/yyyy in C#? 41172 is equivalent to 20/09/2012.

like image 901
Paul Avatar asked Aug 15 '12 11:08

Paul


3 Answers

To go from an DateTime in the "Excel Format" to a C# Date Time you can use the DateTime.FromOADate function.

In your example above:

   DateTime myDate = DateTime.FromOADate(41172);

To write it out for display in the desired format, use:

   myDate.ToString("dd/MM/yyyy");

If you are wondering where the discrepancies in Excel's date handling come from, it's supposed to be on purpose:

When Lotus 1-2-3 was first released, the program assumed that the year 1900 was a leap year even though it actually was not a leap year. This made it easier for the program to handle leap years and caused no harm to almost all date calculations in Lotus 1-2-3.

When Microsoft Multiplan and Microsoft Excel were released, they also assumed that 1900 was a leap year. This allowed Microsoft Multiplan and Microsoft Excel to use the same serial date system used by Lotus 1-2-3 and provide greater compatibility with Lotus 1-2-3. Treating 1900 as a leap year also made it easier for users to move worksheets from one program to the other.

Although it is technically possible to correct this behavior so that current versions of Microsoft Excel do not assume that 1900 is a leap year, the disadvantages of doing so outweigh the advantages.

Source: http://www.ozgrid.com/Excel/ExcelDateandTimes.htm

like image 177
dash Avatar answered Nov 17 '22 22:11

dash


EDIT: As noted in comments and other answers, DateTime.FromOADate is a much better approach. I'll remove this answer if it's ever unaccepted. (But, there are still platforms like .NET Core where FromOADate is not supported so it is still useful for people using these platforms.)

I suspect you want:

DateTime date = new DateTime(1900, 1, 1).AddDays(days - 2);

(See other answers for why you need to subtract 2.)

like image 15
Jon Skeet Avatar answered Nov 17 '22 22:11

Jon Skeet


In case you are looking for FromOADate in .Net Core it is not there.

I dissembled the implementation .Net Framework and created an extension method.

        public static DateTime FromOADate(this double date)
        {
            return new DateTime(DoubleDateToTicks(date), DateTimeKind.Unspecified);
        }

        internal static long DoubleDateToTicks(double value)
        {
            if (value >= 2958466.0 || value <= -657435.0)
                throw new ArgumentException("Not a valid value");
            long num1 = (long)(value * 86400000.0 + (value >= 0.0 ? 0.5 : -0.5));
            if (num1 < 0L)
                num1 -= num1 % 86400000L * 2L;
            long num2 = num1 + 59926435200000L;
            if (num2 < 0L || num2 >= 315537897600000L)
                throw new ArgumentException("Not a valid value");
            return num2 * 10000L;
        }

Still need to test it.

like image 3
Chandermani Avatar answered Nov 17 '22 20:11

Chandermani