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 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]
"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".
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
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
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 :)
"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
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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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)
{...
|
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...
|
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))
|
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...
|
by: Frank Rizzo |
last post by:
Any ideas?
|
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")...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |