简体   繁体   English

如何将DateTime值转换为double?

[英]How can I convert a DateTime value to a double?

如何将DateTime值转换为double

If, by double you mean an OLE Automation date, then you can use DateTime.ToOADate() . 如果,通过double表示OLE自动化日期,则可以使用DateTime.ToOADate() From the linked MSDN topic: 从链接的MSDN主题:

An OLE Automation date is implemented as a floating-point number whose value is the number of days from midnight, 30 December 1899. For example, midnight, 31 December 1899 is represented by 1.0; OLE自动化日期实现为浮点数,其值为1899年12月30日午夜的天数。例如,1899年12月31日午夜由1.0表示; 6 AM, 1 January 1900 is represented by 2.25; 1900年1月1日上午6点由2.25表示; midnight, 29 December 1899 is represented by -1.0; 1899年12月29日午夜以-1.0表示; and 6 AM, 29 December 1899 is represented by -1.25. 1899年12月29日上午6点用-1.25表示。

The base OLE Automation Date is midnight, 30 December 1899. The maximum OLE Automation Date is the same as MaxValue, the last moment of 31 December 9999. 基本OLE自动化日期是1899年12月30日午夜。最大OLE自动化日期与MaxValue相同,即9999年12月31日的最后一刻。

If you're talking about some other date representation that can also be stored in a double , please specify... 如果您正在谈论其他日期表示也可以存储在double ,请指定......

DateTime.ToOADate()转换为C#中的双OLE自动化日期

Yes, OLE Automation date enable Datetime to convert to Decimal/double type. 是,OLE自动化日期启用Datetime转换为十进制/双Datetime类型。 However, the outcome/value to decimal/double is not exact system Datetime . 但是,十进制/双Datetime的结果/值不是精确的系统Datetime

For example, 例如,

Decimal dateIndDec = Convert.Decimal (Datetim.Today.ToOADate());

is not equal to MS SQL 不等于MS SQL

 Select Convert (Decimal (10, 9), GetDate())

Conclusion: OLE Automation date is not a true system datetime info... cannot use it. 结论:OLE自动化日期不是真正的系统datetime信息...无法使用它。

I've been searched everywhere about convert Datetime value to Decimal value in C# without any luck. 我一直在搜索C#中的转换Datetime值到十进制值,没有任何运气。

You can calculate difference between your DateTime and DateTime.MinValue, and then get any Total* value. 您可以计算DateTime和DateTime.MinValue之间的差异,然后获取任何Total*值。

var difference = DateTime.Now - DateTime.MinValue;
Console.WriteLine(difference.TotalMinutes);
Console.WriteLine(difference.TotalMilliseconds);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM