473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why can't a datetime be converted to a 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.

Thanks
Johann

May 28 '07 #1
10 12002
On Mon, 28 May 2007 15:35:47 +0200, Polaris431 <po********@gmail.comwrote:
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

--
Happy coding!
Morten Wennevik [C# MVP]
May 28 '07 #2
"Polaris431" <po********@gmail.comwrote in message
news:11**********************@q66g2000hsg.googlegr oups.com...
>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".

May 28 '07 #3
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
May 28 '07 #4
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

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
May 28 '07 #5
public static double ToDouble(DateTime what)
{
return
BitConverter.ToDouble(BitConverter.GetBytes(what.T icks), 0);
}

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

HTH :)

May 28 '07 #6
"Miroslav Stampar [MCSD.NET / Security+]" <mi**************@gmail.comha
scritto nel messaggio
public static double ToDouble(DateTime what)
{
return
BitConverter.ToDouble(BitConverter.GetBytes(what.T icks), 0);
}
Warning!
Not all bit sequences are valid double numbers.

--

Free .Net Reporting Tool - http://www.neodatatype.net
May 28 '07 #7
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.

May 29 '07 #8
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.

May 29 '07 #9
Fabio wrote:
"Miroslav Stampar [MCSD.NET / Security+]" <mi**************@gmail.comha
scritto nel messaggio
> public static double ToDouble(DateTime what)
{
return
BitConverter.ToDouble(BitConverter.GetBytes(what. Ticks), 0);
}

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.

--
Göran Andersson
_____
http://www.guffa.com
May 29 '07 #10
Polaris431 wrote:
>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.
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
Jun 3 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nikola | last post by:
Hi all, I have a problem converting datetime to integer (and than back to datetime). Depending whether the time is AM or PM, same date is converted to two different integer representations,...
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
3
by: sunny076 | last post by:
Hi, I am trying to convert from Julian to Gregorian data in C#. I was exploring teh JulianCalendar and Gregorian calendar classes but still not sure how I can do it. For example, the Julian date...
8
by: CA | last post by:
Hi, I have a function where I would like to test whether an object is of a certain type. Here is my code so far. public bool HasValidType(Type t, object val) { try { if (t==typeof(double))
8
by: John Slate | last post by:
I have a situation where users will input a start date and an end date, then return the number of weekdays only. Is this possible with the DateTime class? *** Sent via Developersdex...
44
by: Frank Rizzo | last post by:
Any ideas?
11
by: Tim | last post by:
Hi, I am trying to do something simple. Convert a string date to datetime but it is not working and is giving me a baffling error! System.Convert.ToDateTime("Jan 30, 2006")...
12
by: ThunderMusic | last post by:
Hi, We have a part of our application that deals with millions of records and do some processing of them. We've achieved a pretty good performance gain by developping a custom DateTime.ToString...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.