Converting Double to DateTime?

后端 未结 5 2032
醉酒成梦
醉酒成梦 2020-12-10 14:21

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.

5条回答
  •  误落风尘
    2020-12-10 14:50

    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.

提交回复
热议问题