473,398 Members | 2,212 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,398 software developers and data experts.

Major bug in System.Date.ToOADate and FromOADate

ToOADate and FromOADate don't handle negative dates with time parts
correctly. Negative dates with no time parts are OK. (Note: In COM
date "zero" is 12/30/1899).

Does anyone know if these are known bugs and if they will be
corrected? In the meantime I strongly recommend writing your own
functions if you work with negative time. In particular if you've
converted a vb6 project to dotnet.

-----------------------

ToOADate converts negative "round" dates ok :

#12/29/1899#.ToOADate ' = -1 OK
#12/28/1899#.ToOADate ' = -2 OK
#12/27/1899#.ToOADate ' = -3 OK

However negative dates with a time part don't convert correctly :

#12/29/1899 11:59:59 PM#.ToOADate ' = ~-2 NOT OK should be ~0
#12/28/1899 11:59:59 PM#.ToOADate ' = ~-3 NOT OK should be ~-1
#12/27/1899 11:59:59 PM#.ToOADate ' = ~-4 NOT OK should be ~-2

Playing around with other times of day it seems this is because the
code in ToOADate is *subtracting* the time part from the negative date
rather than adding it.
System.DateTime.FromOADate converts negative round numbers ok :

FromOADate(-1) ' = #12/29/1899# OK
FromOADate(-2) ' = #12/28/1899# OK
FromOADate(-2) ' = #12/27/1899# OK
However negative dates with a time part don't convert correctly :

FromOADate(-0.999) ' = ~#12/31/1899# NOT OK, should be ~#12/29/1899#.
FromOADate(-1.999) ' = ~#12/30/1899# NOT OK, should be ~#12/28/1899#.
FromOADate(-2.999) ' = ~#12/29/1899# NOT OK, should be ~#12/27/1899#.

Playing around with other numbers it seems this is because the code in
FromOADate is *adding* the fractional part of the number to the date
rather than subtracting it.
Nov 20 '05 #1
11 10352
"tohear" <ti****@ohear.com> schrieb
ToOADate and FromOADate don't handle negative dates with time
parts correctly. Negative dates with no time parts are OK. (Note: In
COM date "zero" is 12/30/1899).

Does anyone know if these are known bugs and if they will be
corrected? In the meantime I strongly recommend writing your own
functions if you work with negative time. In particular if you've
converted a vb6 project to dotnet.

-----------------------

ToOADate converts negative "round" dates ok :

#12/29/1899#.ToOADate ' = -1 OK
#12/28/1899#.ToOADate ' = -2 OK
#12/27/1899#.ToOADate ' = -3 OK

However negative dates with a time part don't convert correctly :

In VB6, I also misunderstood how it works, and there it worked the same way
as it does now in VB.Net. The simple answer is: It's not a bug, but it's
how it works: :-)
The Integer part is the day based on #12/30/1899#. The fractional part is
the time.

#12/29/1899 11:59:59 PM#.ToOADate ' = ~-2 NOT OK should be ~0
It returns -1.99998842592593. I think this is important in this case.

-1.99998842592593 has to interpreted this way:
-1 is the day: #12/29/1899# minus 1 day = #12/28/1899#
The fractional part is the time: 0.99998842592593 is 11:59:59 PM

That's also true for the other cases.
#12/28/1899 11:59:59 PM#.ToOADate ' = ~-3 NOT OK should be ~-1
#12/27/1899 11:59:59 PM#.ToOADate ' = ~-4 NOT OK should be ~-2

Playing around with other times of day it seems this is because
the code in ToOADate is *subtracting* the time part from the negative
date rather than adding it.
System.DateTime.FromOADate converts negative round numbers ok :

FromOADate(-1) ' = #12/29/1899# OK
FromOADate(-2) ' = #12/28/1899# OK
FromOADate(-2) ' = #12/27/1899# OK ^^ probably -3 :-)
However negative dates with a time part don't convert correctly :

FromOADate(-0.999) ' = ~#12/31/1899# NOT OK, should be
~#12/29/1899#. FromOADate(-1.999) ' = ~#12/30/1899# NOT OK, should be
~#12/28/1899#. FromOADate(-2.999) ' = ~#12/29/1899# NOT OK, should be
~#12/27/1899#.

Playing around with other numbers it seems this is because the code
in FromOADate is *adding* the fractional part of the number to the
date rather than subtracting it.


Well, FromOADate is consistent with ToOADate. The behavior is the same as
explained above.
--
Armin
Nov 20 '05 #2
"tohear" <ti****@ohear.com> schrieb
ToOADate and FromOADate don't handle negative dates with time
parts correctly. Negative dates with no time parts are OK. (Note: In
COM date "zero" is 12/30/1899).

Does anyone know if these are known bugs and if they will be
corrected? In the meantime I strongly recommend writing your own
functions if you work with negative time. In particular if you've
converted a vb6 project to dotnet.

-----------------------

ToOADate converts negative "round" dates ok :

#12/29/1899#.ToOADate ' = -1 OK
#12/28/1899#.ToOADate ' = -2 OK
#12/27/1899#.ToOADate ' = -3 OK

However negative dates with a time part don't convert correctly :

In VB6, I also misunderstood how it works, and there it worked the same way
as it does now in VB.Net. The simple answer is: It's not a bug, but it's
how it works: :-)
The Integer part is the day based on #12/30/1899#. The fractional part is
the time.

#12/29/1899 11:59:59 PM#.ToOADate ' = ~-2 NOT OK should be ~0
It returns -1.99998842592593. I think this is important in this case.

-1.99998842592593 has to interpreted this way:
-1 is the day: #12/29/1899# minus 1 day = #12/28/1899#
The fractional part is the time: 0.99998842592593 is 11:59:59 PM

That's also true for the other cases.
#12/28/1899 11:59:59 PM#.ToOADate ' = ~-3 NOT OK should be ~-1
#12/27/1899 11:59:59 PM#.ToOADate ' = ~-4 NOT OK should be ~-2

Playing around with other times of day it seems this is because
the code in ToOADate is *subtracting* the time part from the negative
date rather than adding it.
System.DateTime.FromOADate converts negative round numbers ok :

FromOADate(-1) ' = #12/29/1899# OK
FromOADate(-2) ' = #12/28/1899# OK
FromOADate(-2) ' = #12/27/1899# OK ^^ probably -3 :-)
However negative dates with a time part don't convert correctly :

FromOADate(-0.999) ' = ~#12/31/1899# NOT OK, should be
~#12/29/1899#. FromOADate(-1.999) ' = ~#12/30/1899# NOT OK, should be
~#12/28/1899#. FromOADate(-2.999) ' = ~#12/29/1899# NOT OK, should be
~#12/27/1899#.

Playing around with other numbers it seems this is because the code
in FromOADate is *adding* the fractional part of the number to the
date rather than subtracting it.


Well, FromOADate is consistent with ToOADate. The behavior is the same as
explained above.
--
Armin
Nov 20 '05 #3
Hello Armin,

I've ran some tests on VB6 and you're correct that it mostly behaves the
same way: 1 second before the end of 12/29/1899 (#12/29/1899 11:59:59
PM#) is in fact considered as 1 second before the start of 12/28/1899
rather than 12/30/1899. So it's "logical" that it should be
-1.99998842592593. I was wrong to believe this was a bug.

However there remains a problem :

in VB6: CDbl(Cdate(-1.5)) = -1.5
and : CDbl(Cdate(-0.5)) = -0.5

In dotnet : FromOADate(-1.5).ToOADate = -1.5
but : FromOADate(-0.5).ToOADate = 0.5 (!!!)

So it's as if doubles smaller than 0 and bigger than -1 are no longer
converted to COM dates properly and instead turned into positive values.

Any suggestions as to how values < 0 and > -1 can still be used?

Thanks,

Tim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4
Hello Armin,

I've ran some tests on VB6 and you're correct that it mostly behaves the
same way: 1 second before the end of 12/29/1899 (#12/29/1899 11:59:59
PM#) is in fact considered as 1 second before the start of 12/28/1899
rather than 12/30/1899. So it's "logical" that it should be
-1.99998842592593. I was wrong to believe this was a bug.

However there remains a problem :

in VB6: CDbl(Cdate(-1.5)) = -1.5
and : CDbl(Cdate(-0.5)) = -0.5

In dotnet : FromOADate(-1.5).ToOADate = -1.5
but : FromOADate(-0.5).ToOADate = 0.5 (!!!)

So it's as if doubles smaller than 0 and bigger than -1 are no longer
converted to COM dates properly and instead turned into positive values.

Any suggestions as to how values < 0 and > -1 can still be used?

Thanks,

Tim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5
I think you will find that if you pass actual date/time values you will be
much happier with the results then if you pass things that acre not actually
dates at all?
--
MichKa [MS]

This posting is provided "AS IS" with
no warranties, and confers no rights.
"Timothy O'Hear" <ti****@ohear.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Hello Armin,

I've ran some tests on VB6 and you're correct that it mostly behaves the
same way: 1 second before the end of 12/29/1899 (#12/29/1899 11:59:59
PM#) is in fact considered as 1 second before the start of 12/28/1899
rather than 12/30/1899. So it's "logical" that it should be
-1.99998842592593. I was wrong to believe this was a bug.

However there remains a problem :

in VB6: CDbl(Cdate(-1.5)) = -1.5
and : CDbl(Cdate(-0.5)) = -0.5

In dotnet : FromOADate(-1.5).ToOADate = -1.5
but : FromOADate(-0.5).ToOADate = 0.5 (!!!)

So it's as if doubles smaller than 0 and bigger than -1 are no longer
converted to COM dates properly and instead turned into positive values.

Any suggestions as to how values < 0 and > -1 can still be used?

Thanks,

Tim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #6
Hello MichKa,

I wouldn't touch OADates with a 10 foot pole if I had the choice.
However the code was originally developed in VB6 so I'm somewhat hostage
to dates becoming doubles that may become dates again, bar a substantial
redesign.

If I've understood this correctly, use of COM dates/times from -23:59:59
to -00:00:01 (which VB6 was perfectly happy with) requires that
To/FromOADate handles the interval ]-1;0[ which it doesn't seem to do.

Would you say that it's by design that FromOADate(-0.5).ToOADate = 0.5
whereas FromOADate(-1.5).ToOADate = -1.5, or is this a bug?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7
"Timothy O'Hear" <ti****@ohear.com> schrieb
Hello Armin,

I've ran some tests on VB6 and you're correct that it mostly behaves
the same way: 1 second before the end of 12/29/1899 (#12/29/1899
11:59:59 PM#) is in fact considered as 1 second before the start of
12/28/1899 rather than 12/30/1899. So it's "logical" that it should
be -1.99998842592593. I was wrong to believe this was a bug.

However there remains a problem :

in VB6: CDbl(Cdate(-1.5)) = -1.5
and : CDbl(Cdate(-0.5)) = -0.5

In dotnet : FromOADate(-1.5).ToOADate = -1.5
but : FromOADate(-0.5).ToOADate = 0.5 (!!!)

So it's as if doubles smaller than 0 and bigger than -1 are no
longer converted to COM dates properly and instead turned into
positive values.

Any suggestions as to how values < 0 and > -1 can still be used?

-0.5 and +0.5 are equal because -0 and +0 are equal. If the day is zero, it
is #12/30/1899#. Then add the fractional part, i.e. #12:00#. Result:
#12/30/1899 12:00#
--
Armin

Nov 20 '05 #8
I think you will find that if you pass actual date/time values you will be
much happier with the results then if you pass things that acre not actually
dates at all?
--
MichKa [MS]

This posting is provided "AS IS" with
no warranties, and confers no rights.
"Timothy O'Hear" <ti****@ohear.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Hello Armin,

I've ran some tests on VB6 and you're correct that it mostly behaves the
same way: 1 second before the end of 12/29/1899 (#12/29/1899 11:59:59
PM#) is in fact considered as 1 second before the start of 12/28/1899
rather than 12/30/1899. So it's "logical" that it should be
-1.99998842592593. I was wrong to believe this was a bug.

However there remains a problem :

in VB6: CDbl(Cdate(-1.5)) = -1.5
and : CDbl(Cdate(-0.5)) = -0.5

In dotnet : FromOADate(-1.5).ToOADate = -1.5
but : FromOADate(-0.5).ToOADate = 0.5 (!!!)

So it's as if doubles smaller than 0 and bigger than -1 are no longer
converted to COM dates properly and instead turned into positive values.

Any suggestions as to how values < 0 and > -1 can still be used?

Thanks,

Tim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #9
Hello MichKa,

I wouldn't touch OADates with a 10 foot pole if I had the choice.
However the code was originally developed in VB6 so I'm somewhat hostage
to dates becoming doubles that may become dates again, bar a substantial
redesign.

If I've understood this correctly, use of COM dates/times from -23:59:59
to -00:00:01 (which VB6 was perfectly happy with) requires that
To/FromOADate handles the interval ]-1;0[ which it doesn't seem to do.

Would you say that it's by design that FromOADate(-0.5).ToOADate = 0.5
whereas FromOADate(-1.5).ToOADate = -1.5, or is this a bug?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #10
"Timothy O'Hear" <ti****@ohear.com> schrieb
Hello Armin,

I've ran some tests on VB6 and you're correct that it mostly behaves
the same way: 1 second before the end of 12/29/1899 (#12/29/1899
11:59:59 PM#) is in fact considered as 1 second before the start of
12/28/1899 rather than 12/30/1899. So it's "logical" that it should
be -1.99998842592593. I was wrong to believe this was a bug.

However there remains a problem :

in VB6: CDbl(Cdate(-1.5)) = -1.5
and : CDbl(Cdate(-0.5)) = -0.5

In dotnet : FromOADate(-1.5).ToOADate = -1.5
but : FromOADate(-0.5).ToOADate = 0.5 (!!!)

So it's as if doubles smaller than 0 and bigger than -1 are no
longer converted to COM dates properly and instead turned into
positive values.

Any suggestions as to how values < 0 and > -1 can still be used?

-0.5 and +0.5 are equal because -0 and +0 are equal. If the day is zero, it
is #12/30/1899#. Then add the fractional part, i.e. #12:00#. Result:
#12/30/1899 12:00#
--
Armin

Nov 20 '05 #11
That is by design. I was about to explain why when I noticed that Armin did
it already.
--
MichKa [MS]

This posting is provided "AS IS" with
no warranties, and confers no rights.
"Timothy O'Hear" <ti****@ohear.com> wrote in message
news:u%***************@TK2MSFTNGP11.phx.gbl...
Hello MichKa,

I wouldn't touch OADates with a 10 foot pole if I had the choice.
However the code was originally developed in VB6 so I'm somewhat hostage
to dates becoming doubles that may become dates again, bar a substantial
redesign.

If I've understood this correctly, use of COM dates/times from -23:59:59
to -00:00:01 (which VB6 was perfectly happy with) requires that
To/FromOADate handles the interval ]-1;0[ which it doesn't seem to do.

Would you say that it's by design that FromOADate(-0.5).ToOADate = 0.5
whereas FromOADate(-1.5).ToOADate = -1.5, or is this a bug?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Omer kamal | last post by:
Dear all, I want to save my data into MS Access database Table which consist of some fields with short date and short time formats. I try to save data in to the said table and I am getting error...
8
by: Tim | last post by:
Does anyone know how to convert a date to the hundred year date format. For example, how would i convert 8/11/2004 to its hundred year date format. Thanks! Tim
0
by: tohear | last post by:
ToOADate and FromOADate don't handle negative dates with time parts correctly. Negative dates with no time parts are OK. (Note: In COM date "zero" is 12/30/1899). Does anyone know if these are...
1
by: Kenny | last post by:
I am having issues with the "Date.ToOADate" method in VB .NET. I understand that in VB6 the function DateSerial gives an internal representation of the date. When I use the DateSerial method in...
2
by: Robin Tucker | last post by:
I have an active-X control (ATl/C++) that expects a date as Variant Time (a double). How can I convert a date in VB.NET into such a double? Any help would be much appreciated. thanks.
4
by: sam | last post by:
Possible convert VB 6 to VB.Net 1.1 for date serial function? Example, Calendar_Date = DateSerial(1900+(Julian_Date/1000), 1,Julian_Date Mod 1000) ? Calendar_Date and Julian_Date are input...
4
by: Peter | last post by:
What is the easiest way to convert serialdate (like excel has) into real date in VB.NET? Thank You Peter
2
by: TofuTheGreat | last post by:
I'm using "Now.ToOADate" for a record timestamp in a small database app (it's what I want to do so don't try to disuade me ;-D). Anyway. I store the value of Now.ToOADate in a string field in...
1
by: =?Utf-8?B?RGFwcGVyRGFuSEBub3NwYW0ubm9zcGFt?= | last post by:
Given the example below, can someone explain why TimeSpan.TotalDays gives a different result than subtracting 2 DateTime.ToOADates? I am completely stumped. Thanks in advance, Dan Example...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
isladogs
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...

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.