Converting a Double to DateTime

O

Ollie

Ok, As the Convert.ToDateTime has not been implimented yet, how do I convert
a double to datetime???

I have looked all over with no luck at all.

Please help me!
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Ollie,

First of all: How do you want to convert double to datetime?
I heard about long integer (millisecond) date/time representation
but i don't hear of any double date/time representation.

Regards

Marcin
 
M

Morten Wennevik

Hi Ollie,

It depends entirely on what information is stored in the double, and how it is stored. A DateTime can be stored and created using a single number (long)

DateTime dt = new DateTime(ticks);

where ticks is the number of 100-nanosecond units from January 1. 0001
 
J

Jon Skeet [C# MVP]

Ollie said:
Ok, As the Convert.ToDateTime has not been implimented yet, how do I convert
a double to datetime???

I have looked all over with no luck at all.

Please help me!

Well, how do you want the conversion to work? What double value do you
have to start with?
 
J

Jon Skeet [C# MVP]

Morten Wennevik said:
Ah, so there is such a thing as a standard double datetime represenation.

Well, if you call OLE Automation representation "standard" :)

(It's defined in quite an odd way, too, if you look at it. What amuses
me is that due to the nature of floating point numbers, you can most
accurately represent dates/times close to 1900 - as you go through
time, the dates/times you can represent grow sparser.)
 
O

Ollie

Guys, Cheers for the responses.

Can't use DateTime dt = New DateTime(ticks) as ticks needs to be a long.

DateTime.FromAODate(double) reports and Ilegal Date exception.

here is an example value of a double I have 5.54533938826208E+129

Cheers

Ollie

Ollie said:
Ok, As the Convert.ToDateTime has not been implimented yet, how do I convert
a double to datetime???

I have looked all over with no luck at all.

Please help me!

Well, how do you want the conversion to work? What double value do you
have to start with?
 
M

Morten Wennevik

You could cast the double into a long, but I doubt you would get the correct date out of it. Where does the double come from?
 
J

Jon Skeet [C# MVP]

Ollie said:
Can't use DateTime dt = New DateTime(ticks) as ticks needs to be a long.

DateTime.FromAODate(double) reports and Ilegal Date exception.

here is an example value of a double I have 5.54533938826208E+129

And what DateTime is that meant to represent?
 
J

Jon Skeet [C# MVP]

Ollie said:
The double is coming from a file which I am using BinaryReader to read. I am
using BinaryReader.ReadDouble()

That's not particularly helpful though - we need to know what the
interpretation of the double is meant to be. What's generating the file
to start with?
 
O

Ollie

This one for example should be as follows...

1.88274989461857E-183 should represent 30/06/2000

Ollie said:
Can't use DateTime dt = New DateTime(ticks) as ticks needs to be a long.

DateTime.FromAODate(double) reports and Ilegal Date exception.

here is an example value of a double I have 5.54533938826208E+129

And what DateTime is that meant to represent?
 
O

Ollie

Unfortunately I cannot disclose that as it is sensitive info.

I have used ACL to determine the type of the data and know that it is a
float (double)

I can tell you that the application that created the file was written in C++

Ollie said:
The double is coming from a file which I am using BinaryReader to read. I am
using BinaryReader.ReadDouble()

That's not particularly helpful though - we need to know what the
interpretation of the double is meant to be. What's generating the file
to start with?
 
J

Jon Skeet [C# MVP]

Ollie said:
This one for example should be as follows...

1.88274989461857E-183 should represent 30/06/2000

Hmm... that sounds very odd, to have a number so *miniscule*
representing a (fairly arbitrary) date. Are you sure the doubles are
being read correctly? (They could be in a different endianness, for
instance.)
 
J

Jon Skeet [C# MVP]

Ollie said:
Unfortunately I cannot disclose that as it is sensitive info.

Then we're unlikely to be able to help you.
I have used ACL to determine the type of the data and know that it is a
float (double)

I can tell you that the application that created the file was written in C++

Are you able to verify that the doubles are being read correctly? (Do
you know what they're *meant* to be in the file, and is your app
reading them as those values?)
 
O

Ollie

I am not sure if my app is reading them correctly. I don't know a great deal
of C# or C++ so am unsure how else I can read them.
The applications that created these files also reads them and picks up the
correct date, but I do not have access the source.

Ollie said:
Unfortunately I cannot disclose that as it is sensitive info.

Then we're unlikely to be able to help you.
I have used ACL to determine the type of the data and know that it is a
float (double)

I can tell you that the application that created the file was written in
C++

Are you able to verify that the doubles are being read correctly? (Do
you know what they're *meant* to be in the file, and is your app
reading them as those values?)
 
G

Gary James

Both Visual Basic and Microsoft Excel use the OLE standard for Date & Time.
A double precision variable holds both a date and time value. Digits left
of the decimal point are days. Digits right of the decimal point are the
decimal part of a day. The OLE standard uses Midnight Dec 31th, 1899 as the
epoch date and time for a value of 0.0. Strangely, if you put "0" in a
cell in Excel, and set the cell custom formatting to "mm/dd/yyyy hh:mm:ss",
Excel displays the date as "Jan 0, 1900" 12:00 AM.

Gary ...
 
J

Jon Skeet [C# MVP]

Ollie said:
I am not sure if my app is reading them correctly. I don't know a great deal
of C# or C++ so am unsure how else I can read them.
The applications that created these files also reads them and picks up the
correct date, but I do not have access the source.

You see, the problem is that they could be written with a different
endianness, or any number of things. If you don't know (or aren't
willing to share) the meaning of the double, I really don't see what
more anyone here can do.
 
O

Ollie

Using VB I can open the file for Binary Access
Open [filename] For Binary Access Read Lock Read Write As #1

Populate my variables with contents of the file.
Get #1, , myType

And then covert the double to a DateTime
Format(myType.myDateDouble, "dd-mm-yyyy")

And it works fine. What is interesting is the value of the double in VB is
different to C#

Does this help a all.

Ollie said:
I am not sure if my app is reading them correctly. I don't know a great deal
of C# or C++ so am unsure how else I can read them.
The applications that created these files also reads them and picks up the
correct date, but I do not have access the source.

You see, the problem is that they could be written with a different
endianness, or any number of things. If you don't know (or aren't
willing to share) the meaning of the double, I really don't see what
more anyone here can do.
 
J

Jon Skeet [C# MVP]

Ollie said:
Using VB I can open the file for Binary Access
Open [filename] For Binary Access Read Lock Read Write As #1

Populate my variables with contents of the file.
Get #1, , myType

And then covert the double to a DateTime
Format(myType.myDateDouble, "dd-mm-yyyy")

And it works fine. What is interesting is the value of the double in VB is
different to C#

Does this help a all.

Well, I don't know enough about how VB works to say for sure, but it
will probably help someone else to help you.

Could you give an example of the value VB reads (as a floating point
number), the value C# reads, and the data in the file?
 

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