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

Huge date bug?

17
Short story: Adding 30 min to 2:00 PM doesn't equal 2:30 PM.

Try this code:

Expand|Select|Wrap|Line Numbers
  1. Dim dTemp1 As Date
  2. Dim dTemp2 As Date
  3.  
  4. dTemp1 = #11/21/2013 2:30:00 PM#
  5. dTemp2 = #11/21/2013 2:00:00 PM#
  6. dTemp2 = DateAdd("n", 30, dTemp2)
  7.  
  8. If dTemp1 = dTemp2 Then
  9.     Debug.Print "ok"
  10. Else
  11.     Debug.Print dTemp1 & " <> " & dTemp2
  12. End If
  13.  
Output: 11/21/2013 2:30:00 PM <> 11/21/2013 2:30:00 PM

You can insert these two equal date/time values into at table indexed to not allow duplicates in that field.

I tried creating the dates by using DateSerial and DateAdd, same result.

Adding 1 min to both times (2:01, 2:31) will produce correct output.

I presume floating numbers are playing a role here, but how can this be dealt with?

MS Access 2010, 2003 and 97.
Nov 29 '13 #1
9 1335
dgunner71
110 100+
Maybe try:

dtTemp1 + .0208333

[30 minutes / 1440 minutes in a day]

Gunner
Nov 29 '13 #2
PPelle
17
Say I have this date in a table, 11/21/2013 2:30:00 PM, calculated with DateAdd. How can I search and find this date? It won't be found. I will have to loop through all the relevant records and use DateDiff to compare each with the search date.

This is not expected behaviour according to this KB: http://support.microsoft.com/kb/210276
Nov 30 '13 #3
NeoPa
32,556 Expert Mod 16PB
This is not expected behaviour according to this KB:
Actually, that's exactly what the linked page does predict. Some literal results may not match the calculated results due to Double-Precision rounding errors. That article explains exactly why you are seeing what you are seeing.

To illustrate further try the following code :
Expand|Select|Wrap|Line Numbers
  1. Debug.Print CDbl(#11/21/13 14:30#)-CDbl(DateAdd('n', 30, #11/21/13 14:00#))
Not a big number to be sure, but nor is it zero.
Nov 30 '13 #4
PPelle
17
Actually, that's exactly what the linked page does predict.
No, one solution the article recommends is this:

Add an associated date to the time comparison:
var1 = #1/1/99 2:01:00 PM#
var2 = DateAdd("n", 10, var1)
? var2 = #1/1/99 2:11:00 PM#
Subtract 1 min from line two and three, and you will get an False where it should be True. Try yourself in the Immediate window.
Nov 30 '13 #5
ADezii
8,834 Expert 8TB
Using a more precise Data Type (Decimal) will give you the OK that you are looking for:
Expand|Select|Wrap|Line Numbers
  1. Dim varT1 As Variant
  2. Dim varT2 As Variant
  3.  
  4.  
  5. varT1 = CDec(#11/21/2013 2:30:00 PM#)
  6. varT2 = #11/21/2013 2:00:00 PM#
  7. varT2 = CDec(DateAdd("n", 30, varT2))
  8.  
  9. If varT1 = varT2 Then    'will now show equality
  10.   MsgBox "ok"
  11. Else
  12.   MsgBox varT1 & " <> " & varT2
  13. End If
P.S. - Modifying NeoPa's example in Post# 4 but converting to Decimal, the Expression
Expand|Select|Wrap|Line Numbers
  1. Debug.Print CDec(#11/21/2013 2:30:00 PM#) - CDec(DateAdd("n", 30, #11/21/2013 2:00:00 PM#))
  2.  
will evaluate to 0.
Nov 30 '13 #6
zmbd
5,501 Expert Mod 4TB
Start out with dtemp1 = dtemp2
use the dateadd to add the 30 minutes to each
then compare.
Expand|Select|Wrap|Line Numbers
  1. Sub x()
  2. Dim dTemp1 As Date
  3. Dim dTemp2 As Date
  4.  
  5. dTemp1 = #11/21/2013 2:30:00 PM#
  6. dTemp2 = #11/21/2013 2:00:00 PM#
  7. dTemp1 = DateAdd("n", 30, dTemp1)
  8. dTemp2 = DateAdd("n", 30, dTemp2)
  9.  
  10. If dTemp1 = dTemp2 Then Debug.Print "good"
  11.  
  12. End Sub
  13.  
Need to convert the dates to serial to take a look at them... maybe later... kids in the tub (^_^)
Nov 30 '13 #7
NeoPa
32,556 Expert Mod 16PB
@PPelle.
Compare time data
When you compare time values, you may receive inconsistent results because a time value is stored as the fractional part of a double-precision, floating-point number. For example, if you type the following expression in the Immediate window, you receive a false (0) result even though the two time values look the same:
Expand|Select|Wrap|Line Numbers
  1. var1 = #2:01:00 PM#
  2. var2 = DateAdd("n", 10, var1)
  3. ? var2 = #2:11:00 PM#
When Access converts a time value to a fraction, the calculated result may not be identical to the time value. The small difference caused by the calculation is sufficient to produce a false (0) result when you compare a stored value to a constant value.
Are you sure you want to deny the obvious truth that the article does, indeed, explain exactly what you have reported as a problem. Strangely, the explanation matches the illustration I provided earlier very closely.

Just after this section in the article it gives examples of ways to ensure you don't have this problem. ADezii's solution, though similar to a couple of them, is another technique that would work for you.

Subtract 1 min from line two and three, and you will get an False where it should be True. Try yourself in the Immediate window.
These instructions are too ambiguous for me to be sure I followed them correctly, but :
A) I did follow them as clearly as they are. I found that the result was still True. The exact code I ran was :
Expand|Select|Wrap|Line Numbers
  1. var1 = #1/1/99 2:00:00 PM#
  2. var2 = DateAdd("n", 10, var1)
  3. ? var2 = #1/1/99 2:10:00 PM#
B) Regardless of the result, this has no bearing on the statement you made denying that the article does warn readers of this known issue when comparing time values.

I'm guessing you didn't even bother to run the line of code I suggested you run. I would expect the results of that line to show clearly why the issue you reported is, in fact, quite predictable.
Nov 30 '13 #8
dgunner71
110 100+
PPelle,

I just created a quick query to test the add function. Because Access reads a date as whole numbers and the time as the decimal portion you can very easily add portions of days by adding times such as .0208.

In query screenshots below, I've created a new value [fxDate] which adds the value I want (i.e. 30 minutes or .0208 of a day). If I only add the value, I will get a mixed number - for this reason, I have to change that mixed number back to a date using the CvDate Function.

You should be able to use this value in your search criteria if you like.

Gunner



Attached Images
File Type: png Query1.PNG (3.6 KB, 315 views)
File Type: png Query2.PNG (19.5 KB, 316 views)
Nov 30 '13 #9
PPelle
17
Thanks for all suggestions, I will need some time to try them all and give you proper feedback.
Nov 30 '13 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ralph Freshour | last post by:
What's a good way to calculate the number of days between two dates in the following format: 2003-07-15 2003-08-02 I've looked at the PHP date functions but I'm still a bit lost...
3
by: Adrian Parker | last post by:
I have a server app which is sent a date in the local format of the client machine. I've no control over the client app at all, so have to code at the server end to cope with any problems. The...
4
by: Gord | last post by:
Hello, VB6 accepts Date and Time values as 'Date'. I'm trying to verify entry into a database I'm creating by verifying that an appropriate Date or Time value has been entered. Using built-in...
15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
1
by: Laurence Neville | last post by:
This is regarding a change in the Short Date format under Hebrew Regional Settings, that has caused huge problems in our ASP web application. The change appears to have been introduced sometime...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
9
by: User | last post by:
Hi, Assuming the date format is in 31-Feb-2006 <head> <script language="javascript"> function t() { mill=new Date(2006, 01, 31) ;
3
by: vikas000000a | last post by:
Hi All, I am new to this forum as far as posting a question is concerned, although I have quite frequently visiting this site as a guest. My quertion relates to preforming fast searched in a very...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.