Why can't a datetime be converted to a double?

P

Polaris431

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
 
M

Morten Wennevik [C# MVP]

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

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
 
A

Alberto Poblacion

Polaris431 said:
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.

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".
 
M

Michael Nemtsev

Hello Polaris431,

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

Use DateTime.ToOADate with 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

P> I get a casting error when I execute the following:
P>
P> DateTime d = DateTime.Now;
P> Double f = Convert.ToDouble(d);
P> Why? The Convert.ToDouble has many overloads, one of them being the
P> ability to convert a datetime value to a double.
P>
P> Thanks
P> Johann
 
M

Michael Nemtsev

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

P> I get a casting error when I execute the following:
P>
P> DateTime d = DateTime.Now;
P> Double f = Convert.ToDouble(d);
P> Why? The Convert.ToDouble has many overloads, one of them being the
P> ability to convert a datetime value to a double.
P>
P> Thanks
P> Johann
 
M

Miroslav Stampar [MCSD.NET / Security+]

public static double ToDouble(DateTime what)
{
return
BitConverter.ToDouble(BitConverter.GetBytes(what.Ticks), 0);
}

public static DateTime ToDateTime(double what)
{
return new
DateTime(BitConverter.ToInt64(BitConverter.GetBytes(what), 0));
}

HTH :)
 
F

Fabio

public static double ToDouble(DateTime what)
{
return
BitConverter.ToDouble(BitConverter.GetBytes(what.Ticks), 0);
}

Warning!
Not all bit sequences are valid double numbers.
 
P

Polaris431

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".

Yeah, that sounds like the most logical reason.
 
P

Polaris431

How would a date and time be represented as a double?

Internally, time is always stored as a number. So who cares whether
it's an Int, Double or any other numeric data type. Like Alberto has
indicated, the overloading with a convert to double is really a bug
disguised as a feature.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Fabio said:
Warning!
Not all bit sequences are valid double numbers.

Also, not all bit sequences produce unique values as a double. If the
conversion "works" one way, it might not convert back to the same value.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Polaris431 said:
Internally, time is always stored as a number. So who cares whether
it's an Int, Double or any other numeric data type. Like Alberto has
indicated, the overloading with a convert to double is really a bug
disguised as a feature.

The time number is really an integer type.

If you just want the same value as double then you
can just assign it to a double.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top