如何在c#中将double值转换为DateTime?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13919641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:06:00  来源:igfitidea点击:

How to convert a double value to a DateTime in c#?

c#exceldatetimeconvertertimespan

提问by fifamaniac04

I have the value 40880.051388 and am storing it as a double, if I open Excel and paste in a cell and apply the following custom format "m/d/yyyy h:mm"to that cell, I get "12/3/2011 1:14"

我的值为 40880.051388 并将其存储为双精度值,如果我打开 Excel 并粘贴到一个单元格中并将以下自定义格式应用m/d/yyyy h:mm"到该单元格,我会得到"12/3/2011 1:14"

How can I do this parsing/Conversion in C#? I don't know if the value is milliseconds from a certain checkpoint, like epoch time, or if the value is in some specific prepared format, but how does excel come up with this particular value? Can it be done in C#?

如何在 C# 中进行解析/转换?我不知道该值是否是某个检查点的毫秒数,例如纪元时间,或者该值是否采用某种特定的准备格式,但是 excel 是如何得出这个特定值的?可以在 C# 中完成吗?

I've tried working with TimeSpan, DateTime, and other like things in Visual Studio but am not getting anywhere.

我试过在 Visual Studio 中使用TimeSpanDateTime和其他类似的东西,但没有任何进展。

采纳答案by Jaime Torres

Looks like you're using the old OLE Automation date. Use

看起来您正在使用旧的OLE 自动化 date。用

DateTime.FromOADate(myDouble)

回答by Rahul Tripathi

Try something like this:-

尝试这样的事情:-

double d = 40880.051388 ;
DateTime dt = DateTime.FromOADate(d);

回答by Cory Nelson

The value is an offset in days from December 30th, 1899. So you want:

该值是从 1899 年 12 月 30 日开始的以天为单位的偏移量。因此您需要:

new DateTime(1899, 12, 30).AddDays(40880.051388)

回答by Jeppe Stig Nielsen

Try using var dateTime = DateTime.FromOADate(40880.051388);.

尝试使用var dateTime = DateTime.FromOADate(40880.051388);.

If you need to format it to a string, use dateTime.ToString("M/d/yyyy H:mm", CultureInfo.InvariantCulture)for that. That will give you 24-hour string (change Hto hfor a 12-hour system).

如果您需要将其格式化为字符串,请使用dateTime.ToString("M/d/yyyy H:mm", CultureInfo.InvariantCulture)它。这会给你24小时的字符串(变更Hh了12小时制)。

If you need greater precision (by a factor 1000 or more) than offered by FromOADate, see my answer in another thread.

如果您需要比 提供的精度更高(1000 倍或更多)FromOADate,请参阅我在另一个线程中的回答

回答by Kevin O'Shaughnessy

The following simple code will work

以下简单的代码将起作用

DateTime.FromOADate(myDouble)

However if performance is critical, it may not run fast enough. This operation is very processor intensive because the range of dates for the OLE Automation Date format begins on 30 December 1899 whereas DateTime begins on January 1, 0001, in the Gregorian calendar.

但是,如果性能至关重要,它可能运行得不够快。此操作占用大量处理器资源,因为 OLE 自动化日期格式的日期范围从 1899 年 12 月 30 日开始,而 DateTime 从公历中的 0001 年 1 月 1 日开始。

FromOADate calls a DoubleDateToTicks function using myDouble as the only argument. This returns the number of ticks, and this value is used to create a new DateTime with unspecified DateTimeKind.

FromOADate 使用 myDouble 作为唯一参数调用 DoubleDateToTicks 函数。这将返回刻度数,该值用于创建一个具有未指定 DateTimeKind 的新 DateTime。

The vast bulk of this work is done by the DoubleDateToTicks function in mscorlib. This includes code to throw an ArgumentException when the value of the double is NaN, and there are numerous ways in which it can be performance optimized depending on your exact needs.

这项工作的大部分是由 mscorlib 中的 DoubleDateToTicks 函数完成的。这包括在 double 的值为 NaN 时抛出 ArgumentException 的代码,并且有多种方法可以根据您的确切需求对其进行性能优化。