Jump to content

Issue with _DateDiff()


Go to solution Solved by SmOke_N,

Recommended Posts

Writing email alert code where I wanted to differentiate between today, tomorrow and other dates. Simple enough code but when I put it into practice I was getting the wrong alerts.
Did some tests in a temp.au3 and proved my code. Put it back into practise and still getting wrong results. Went back to my test.au3 and eventually found proof that the error lives with _DateDiff(). Here's my POC code, You'll see the last three lines get the wrong result 1, 1 and 2 but should be 0, 0, and 1. You'll see the last three lines get the wrong result 0, 0, and 1 but should be 1, 1 and 2.

Have I done something wrong?

 

#include <Date.au3>
ConsoleWrite("[" & _DateDiff("D", "2024/02/04 00:00:21", "2024/02/04 12:59:21") & "] [sb=0]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/04 00:00:21", "2024/02/05 12:59:21") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/04 00:00:21", "2024/02/06 12:59:21") & "] [sb=2]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/04 12:00:21", "2024/02/06 20:59:21") & "] [sb=2]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/05 17:52:48", "2024/02/06 13:30:00") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/06 17:52:48", "2024/02/07 13:30:00") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/06 17:52:48", "2024/02/08 13:30:00") & "] [sb=2]" & @CRLF)

#cs
+>06:28:13 AU3Check ended.rc:0
>Running:(3.3.16.1):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\User\Documents\AutoIt\test3.au3"
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
[0] [sb=0]
[1] [sb=1]
[2] [sb=2]
[2] [sb=2]
[0] [sb=1]
[0] [sb=1]
[1] [sb=2]
+>06:28:13 AutoIt3.exe ended.rc:0
+>06:28:13 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 1.537
#ce

 

Edited by pseakins
Added strikethrough to clarify the edit changes.

Phil Seakins

Link to comment
Share on other sites

1 hour ago, pseakins said:

Have I done something wrong?

AFAIK : With the parameter "D", _DateDiff only processes the difference of the daily values even if there are less than 24 hours between them. One option would be to use the parameter "s", divide it by 86400 (seconds per day) and take the INT part.

#include <Date.au3>
ConsoleWrite("[" & _DateDiff("D", "2024/02/04 00:00:21", "2024/02/04 12:59:21") & "] [sb=0]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/04 00:00:21", "2024/02/05 12:59:21") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/04 00:00:21", "2024/02/06 12:59:21") & "] [sb=2]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/04 12:00:21", "2024/02/06 20:59:21") & "] [sb=2]" & @CRLF)

;last three lines get the wrong result 1, 1 and 2 but should be 0, 0, and 1.
ConsoleWrite("[" & _DateDiff("D", "2024/02/05 17:52:48", "2024/02/06 13:30:00") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/06 17:52:48", "2024/02/07 13:30:00") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/06 17:52:48", "2024/02/08 13:30:00") & "] [sb=2]" & @CRLF)

; New :
ConsoleWrite("New : ------------------------------------- " & @CRLF)
ConsoleWrite("[" & Int(_DateDiff("s", "2024/02/05 17:52:48", "2024/02/06 13:30:00") / 86400) & "] [sb=0]" & @CRLF)
ConsoleWrite("[" & Int(_DateDiff("s", "2024/02/06 17:52:48", "2024/02/07 13:30:00") / 86400) & "] [sb=0]" & @CRLF)
ConsoleWrite("[" & Int(_DateDiff("s", "2024/02/06 17:52:48", "2024/02/08 13:30:00") / 86400) & "] [sb=1]" & @CRLF)

 

Edited by Musashi
Answer extended

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

56 minutes ago, Musashi said:

One option would be to use the parameter "s", divide it by 86400 (seconds per day) and take the INT part.

I edited my original post as I had shown the wrong sb and actual results (dyslexia, I reversed them) after you copied into your code. The first two comparisons are for the next day so the difference sb 1, the third test is for 2 days apart so result should be 2.

Tried again with your suggestion and still get wrong results exactly the same as the "D" comparison. Look like a lot more conditionals are required.

#include <Date.au3>
; New :
ConsoleWrite("[" & Int(_DateDiff("s", "2024/02/05 17:52:48", "2024/02/06 13:30:00") / 86400) & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & Int(_DateDiff("s", "2024/02/06 17:52:48", "2024/02/07 13:30:00") / 86400) & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & Int(_DateDiff("s", "2024/02/06 17:52:48", "2024/02/08 13:30:00") / 86400) & "] [sb=2]" & @CRLF)
#cs
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
[0] [sb=1]
[0] [sb=1]
[1] [sb=2]
+>11:11:47 AutoIt3.exe ended.rc:0
#ce

 

Edited by pseakins

Phil Seakins

Link to comment
Share on other sites

  • Moderators
ConsoleWrite("[" & _DateDiff("D", "2024/02/05 17:52:48", "2024/02/06 13:30:00") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/06 17:52:48", "2024/02/07 13:30:00") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/06 17:52:48", "2024/02/08 13:30:00") & "] [sb=2]" & @CRLF)

This code is returning the correct response.  You have it as it should return 1 day, 1 day, and 2 days.

However, the first two are less than 24 hours apart from each other and the 3rd is less than 48 hours apart.

The code should return [sb=0], [sb=0], [sb=1].  Days are based on 24 hours not to perspective time.

 

Others have found some issues in the past, or refined the concept to their own needs, if you need something different, you can look at this to see if you can make something more to your own intentions.

 

 

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

3 hours ago, SmOke_N said:

This code is returning the correct response.  You have it as it should return 1 day, 1 day, and 2 days.

I disagree. The difference between the 2nd of March and the 3rd of March, in days, is one day. Time of day has nothing to do with it. The _DateDiff() function should not consider time of day when comparing days with the "D" option.

imagine it is 3pm on Tuesday and you are going to meet with a friend on Wednesday at 9am. You don't say to him "see you today", you say "see you tomorrow", because it is one day away even though it is less that 24 hours.

Anyway I found the issue. Date.au3 on line 254 should be changed from;-

    If $asStartTimePart[0] > 1 And $asEndTimePart[0] > 1 Then
to
    If $sType <> "d" And $asStartTimePart[0] > 1 And $asEndTimePart[0] > 1 Then

A workaround, to avoid changing the library, is to neutralise the time portion of the two comparison strings by setting them both to "12:00:00" or any other value.

 

Phil Seakins

Link to comment
Share on other sites

  • Moderators
  • Solution

Unfortunately, it's not up to our perspective on how something someone else wrote works.  It's a fact that 24 hours is 1 day, you're using Hour:Minute:Second in your code, therefore your narrowing down the "time" to specifics.  

If you want to make it work the way you want take out the H:M:S part of the code.

#include <Date.au3>
ConsoleWrite("[" & _DateDiff("D", "2024/02/05", "2024/02/06") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/06", "2024/02/07") & "] [sb=1]" & @CRLF)
ConsoleWrite("[" & _DateDiff("D", "2024/02/06", "2024/02/08") & "] [sb=2]" & @CRLF)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

12 hours ago, SmOke_N said:

Unfortunately, it's not up to our perspective on how something someone else wrote works. 

Yes, this is quite true.

12 hours ago, SmOke_N said:

If you want to make it work the way you want take out the H:M:S part of the code.

You are absolutely right. I had not tried this because help for _DateDiff() appeared to require both date and time in the input fields. However, in looking more closely I see that the time field is optional and I had not noticed this. I tried your suggestion and it works exactly as expected.

So, this was a storm in a teacup, a non-issue. Thanks all.

Edited by pseakins
Typos

Phil Seakins

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...