Array of double, must be also able to hold DateTime

S

Sonnich Jensen

Hi all

I need a way to have a

public List<double> XValues = new List<double>();

which also can hold DateTime values.

How can I convert a double to DateTime and viseversa?
I tried today, but did not find a proper way

Thx
Sonnich
 
J

Jeff Gaines

Hi all

I need a way to have a

public List<double> XValues = new List<double>();

which also can hold DateTime values.

How can I convert a double to DateTime and viseversa?
I tried today, but did not find a proper way

Thx
Sonnich

DateTime.ToBinary() gets you to an Int64, would that help?
 
R

Rick Lones

Sonnich said:
Hi all

I need a way to have a

public List<double> XValues = new List<double>();

which also can hold DateTime values.

How can I convert a double to DateTime and viseversa?
I tried today, but did not find a proper way

Thx
Sonnich

Well, if your double represents an offset in (100ns) ticks from some known
starting date/time, then that would be a way. But a long int would be more
appropriate since the entire 64 bits would be available. With a double you must
restrict yourself to no more significant bits than the mantissa can accomodate.

-rick-
 
S

Sonnich Jensen

It can be done, per the other two replies and even other techniques.

But I would advise against it.  If you are trying to store values of
type double and DateTime in the same data structure and yet have no
readily convenient way to convert between double and DateTime, then you
have a fundamental design error.

Instead of trying to store two completely unrelated types in the same
data structure, you should fix your design so that doing so isn't necessary.

If you would explain at a higher level what it is you're actually trying
to do, then we can assist you with that.

Pete

Thanks to all of you.

I found my solution in ToOADate() and a few additional functions.
I have a database, where I have my dates in Deplhi format, and I guess
that ToOADate() and FromOADate() can be useful.

From delphi help:
Most VCL objects represent date and time values using the TDateTime
type. The integral part of a TDateTime value is the number of days
that have passed since 12/30/1899. The fractional part of a TDateTime
value is fraction of a 24 hour day that has elapsed.

Following are some examples of TDateTime values and their
corresponding dates and times:

0 12/30/1899 12:00 am
2.75 1/1/1900 6:00 pm
-1.25 12/29/1899 6:00 am
35065 1/1/1996 12:00 am
 
R

Rick Lones

Sonnich said:
Thanks to all of you.

I found my solution in ToOADate() and a few additional functions.
I have a database, where I have my dates in Deplhi format, and I guess
that ToOADate() and FromOADate() can be useful.

From delphi help:
Most VCL objects represent date and time values using the TDateTime
type. The integral part of a TDateTime value is the number of days
that have passed since 12/30/1899. The fractional part of a TDateTime
value is fraction of a 24 hour day that has elapsed.

Following are some examples of TDateTime values and their
corresponding dates and times:

0 12/30/1899 12:00 am
2.75 1/1/1900 6:00 pm
-1.25 12/29/1899 6:00 am
35065 1/1/1996 12:00 am

Not recommended. I recall a problem with VB3, which stored dates as floats
using some such similar scheme. Because floating-point fractional values do not
always have a precise representation it could be the case that if you took a
starting datetime and added, e.g., 5 seconds to it you would not necessarily get
the same result (meaning one which compared as equal in an IF statement) as you
would by taking the same starting datetime and, e.g., adding 4 seconds to it and
then adding 1 second. I considered this a bug, although the MS support person I
reported it to did not . . . YMMV on that, but this can be the kind of issue you
may buy into with such an innapropriate (IMO) representation.

-rick-
 
J

James A. Fortune

Thanks to all of you.

I found my solution in ToOADate() and a few additional functions.
I have a database, where I have my dates in Deplhi format, and I guess
that ToOADate()  and FromOADate() can be useful.

From delphi help:
Most VCL objects represent date and time values using the TDateTime
type. The integral part of a TDateTime value is the number of days
that have passed since 12/30/1899. The fractional part of a TDateTime
value is fraction of a 24 hour day that has elapsed.

Following are some examples of TDateTime values and their
corresponding dates and times:

0       12/30/1899 12:00 am
2.75    1/1/1900 6:00 pm
-1.25   12/29/1899 6:00 am
35065   1/1/1996 12:00 am

In the software world, it's generally considered bad form to rely on
the underlying implementation of the date storage. I.e., in general,
it's good not to violate the principle of encapsulation.
Nevertheless, Microsoft has violated that maxim in order to allow
programmers to skirt using something like a DateDiff function when
comparing dates in SQL queries. The chances are almost nil that
Microsoft is going to change the underlying representation of a date
value during this century, but purists would recommend not taking
advantage of the existing internal floating-point representation. I
generally use comparison functions that expect date formats when
comparing dates, but I'm one of those purists I mentioned :). Even
operator overloading brings up the same issues. The floating point
precision, though, is not really an issue at all when simply comparing
dates, even when comparing to the second (about 0.00001157 of a day).
Mathematically, this is because a conversion to floating-point forms a
partially ordered set (poset) where the corresponding values are
isomorphic to the poset of date/time values (i.e., they maintain the
same relative order as the original date/time values even if the
conversion to number of days plus fraction of a day is not quite
exact). I recommend using the date format for dates and using date
comparisons, ignoring the internal representation. Even if Microsoft
decides to change the internal representation of dates, they will be
responsible for updating any date comparison functions at the same
time, and no changes to your code should be required. Of course, you
are free to determine your own risk comfort level and relaxed degree
of pedantry if adherence to conventional standards of programming is
too onerous in this situation.

James A. Fortune
(e-mail address removed)
 
U

Ulrik Magnusson

I need a way to have a
public List<double> XValues = new List<double>();

which also can hold DateTime values.

Why do you need that? Maybe there is some other way to solve your
problem?
 

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