将Double转换为DateTime? [英] Converting Double to DateTime?

查看:168
本文介绍了将Double转换为DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.CSV文件,我正在读入一个C#程序。在其中一列中,有一个日期,但它是一般格式,所以它显示在.CSV作为一个数字。例如:41172.



如何将此数字转换为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆