为什么日期时间不能转换为双倍? [英] Why can't a datetime be converted to a double?

查看:88
本文介绍了为什么日期时间不能转换为双倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行以下操作时出现转换错误:


DateTime d = DateTime.Now;

Double f = Convert.ToDouble(d) ;


为什么? Convert.ToDouble有很多重载,其中一个是将日期时间值转换为双倍的

能力。


谢谢

Johann

I get a casting error when I execute the following:

DateTime d = DateTime.Now;
Double f = Convert.ToDouble(d);

Why? The Convert.ToDouble has many overloads, one of them being the
ability to convert a datetime value to a double.

Thanks
Johann

推荐答案

2007年5月28日星期一15:35:47 +0200,Polaris431< po **** ****@gmail.com写道:
On Mon, 28 May 2007 15:35:47 +0200, Polaris431 <po********@gmail.comwrote:

执行以下操作时出现转换错误:


DateTime d = DateTime.Now;

Double f = Convert.ToDouble(d);


为什么? Convert.ToDouble有很多重载,其中一个是将日期时间值转换为双倍的

能力。


谢谢

Johann

I get a casting error when I execute the following:

DateTime d = DateTime.Now;
Double f = Convert.ToDouble(d);

Why? The Convert.ToDouble has many overloads, one of them being the
ability to convert a datetime value to a double.

Thanks
Johann



约翰,


如何将日期和时间表示为双倍?如果你需要将值存储在除DateTime之外的任何东西(可能代表一个字符串),请尝试DateTime.Ticks


-

快乐的编码!

Morten Wennevik [C#MVP]

Hi Johann,

How would a date and time be represented as a double? If you need to store the value in anything other than as a DateTime (possibly representedas a string), try DateTime.Ticks

--
Happy coding!
Morten Wennevik [C# MVP]


" Polaris431" < po ******** @ gmail.comwrote in message

news:11 ********************** @ q66g2000hsg.googlegr oups.com ...
"Polaris431" <po********@gmail.comwrote in message
news:11**********************@q66g2000hsg.googlegr oups.com...

>执行以下操作时出现转换错误:


DateTime d = DateTime.Now;

Double f = Convert.ToDouble(d);


为什么? Convert.ToDouble有很多重载,其中一个是将日期时间值转换为double的

能力。
>I get a casting error when I execute the following:

DateTime d = DateTime.Now;
Double f = Convert.ToDouble(d);

Why? The Convert.ToDouble has many overloads, one of them being the
ability to convert a datetime value to a double.



来自Convert.ToDouble(DateTime)的MSDN文档:


"返回值

不支持此转换。没有返回任何价值。


所以你的答案是为什么?就是转换的实现者选择

不支持这种转换,然后记录他们的失败,从而将其从错误中转移出来。转换为功能。

From the MSDN documentation for Convert.ToDouble(DateTime):

"Return Value
This conversion is not supported. No value is returned."

So the answer to your "Why?" is that the implementors of "Convert" chose
to not support this conversion, and then document their failure, thereby
turning it from a "bug" into a "feature".


Hello Polaris431,


Convert.ToDouble(DateTime)始终抛出InvalidCastException :)所以,请问

MS家伙为什么这样做:)


使用DateTime.ToOADate返回双倍


---

WBR,Michael Nemtsev [.NET / C#MVP]。

我的博客: http://spaces.live.com/laflour

团队博客: http://devkids.blogspot.com/

最大的危险我们大多数人并不是说我们的目标太高而且我们错过了它,但它太低了我们达到了它的目标。 (c)米开朗基罗


PI执行以下操作时出现投射错误:

P>

PDateTime d = DateTime。现在;

PDouble f = Convert.ToDouble(d);

PWhy? Convert.ToDouble有很多重载,其中一个是

将datetime值转换为double的能力。

P>

PThanks

PJohann
Hello Polaris431,

Convert.ToDouble(DateTime) always throws InvalidCastException :) So, ask
MS guys why they did this :)

Use DateTime.ToOADate which returns the double

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

PI get a casting error when I execute the following:
P>
PDateTime d = DateTime.Now;
PDouble f = Convert.ToDouble(d);
PWhy? The Convert.ToDouble has many overloads, one of them being the
Pability to convert a datetime value to a double.
P>
PThanks
PJohann


这篇关于为什么日期时间不能转换为双倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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