425,606 Members | 2,019 Online
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

# Converting a Double to DateTime

 P: n/a 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! Nov 16 '05 #1
20 Replies

 P: n/a 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 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! Nov 16 '05 #2

 P: n/a 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 -- Happy coding! Morten Wennevik [C# MVP] Nov 16 '05 #3

 P: n/a Use the static method: DateTime.FromOADate() Arild "Ollie" wrote in message news:uD**************@TK2MSFTNGP11.phx.gbl... 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! Nov 16 '05 #4

 P: n/a Ollie wrote: 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? -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #5

 P: n/a Ah, so there is such a thing as a standard double datetime represenation. -- Happy coding! Morten Wennevik [C# MVP] Nov 16 '05 #6

 P: n/a Morten Wennevik wrote: 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.) -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #7

 P: n/a 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 "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... Ollie wrote: 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? -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #8

 P: n/a 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? -- Happy coding! Morten Wennevik [C# MVP] Nov 16 '05 #9

 P: n/a Ollie wrote: 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? -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #10

 P: n/a Ollie wrote: 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? -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #11

 P: n/a This one for example should be as follows... 1.88274989461857E-183 should represent 30/06/2000 "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... Ollie wrote: 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? -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #12

 P: n/a 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++ "Jon Skeet [C# MVP]" wrote in message news:MP***********************@msnews.microsoft.co m... Ollie wrote: 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? -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #13

 P: n/a Ollie wrote: 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.) -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #14

 P: n/a Ollie wrote: 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?) -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #15

 P: n/a 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. "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... Ollie wrote: 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?) -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #16

 P: n/a 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 ... "Morten Wennevik" wrote in message news:opr91kmdpwklbvpo@morten_x.edunord... Ah, so there is such a thing as a standard double datetime represenation. -- Happy coding! Morten Wennevik [C# MVP] Nov 16 '05 #17

 P: n/a Ollie wrote: 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. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #18

 P: n/a 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. "Jon Skeet [C# MVP]" wrote in message news:MP************************@msnews.microsoft.c om... Ollie wrote: 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. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #19

 P: n/a Ollie wrote: 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? -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #20