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

  • Thread starter Thread starter Polaris431
  • Start date Start date
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
 
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
 
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".
 
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
 
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
 
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 :)
 
public static double ToDouble(DateTime what)
{
return
BitConverter.ToDouble(BitConverter.GetBytes(what.Ticks), 0);
}

Warning!
Not all bit sequences are valid double numbers.
 
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.
 
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.
 
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.
 
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
 
Back
Top