473,387 Members | 1,520 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,387 software developers and data experts.

Converting a Double to DateTime

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 43449
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
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
Use the static method:

DateTime.FromOADate()
Arild

"Ollie" <ol**********@hotmail.com> 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
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Ah, so there is such a thing as a standard double datetime represenation.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6
Morten Wennevik <Mo************@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
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
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
This one for example should be as follows...

1.88274989461857E-183 should represent 30/06/2000

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
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]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #15
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #16
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" <Mo************@hotmail.com> 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
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #18
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]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19
Ollie <ol**********@hotmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #20
Now I feel really silly.

I have taken a closer look at the binary file and noticed I was reading the
wrong section of bytes form the file.

It now works a treat.

Thanks for all the help. Your resonses have given me a better understanding
and I am sure it helped find my error.

Sorry guys.

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

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,...
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...
4
by: M. Uppal | last post by:
How do i convert fraction to time in c#. I need to convert 0.25 to time. Excel does this by using Selection.NumberFormat = "h:mm:ss;@" of the Cell Format function. thanks, M. Uppal
1
by: John Dann | last post by:
I need to convert some datetime data currently stored in .Net DateTime type to the older double style (for compatibility with a legacy control). Is there an intrinsic function to do this or, if...
3
by: spdude | last post by:
I am working on an application that stores date in DOS format. For ex: 9540 which appears to be the number of days since 1980. Anyone knows a way to convert this into the mmddyyyy format? Thanks...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.