将Double转换为DateTime? [英] Converting Double to DateTime?
问题描述
如何将此数字转换为C#格式为dd / mm / yyyy的日期? 41172相当于20/09/2012。
编辑:如评论和其他答案所述, DateTime.FromOADate
是一个更好的方法。如果没有被接受,我会删除这个答案。 (但是,仍然有.NET Core的平台,其中 FromOADate
不支持,因此对于使用这些平台的人来说,这仍然是有用的。)
我怀疑你想要的:
DateTime date = new DateTime(1900,1,1).AddDays天-2);
(查看其他答案,为什么需要减去2。)
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.
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.)
这篇关于将Double转换为DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!