Using DateDiff and getting errors

Gr3g0ry

Registered User.
Local time
Yesterday, 18:20
Joined
Oct 12, 2017
Messages
163
1700972457354.png

as you can see, the rental date and return date(wich is the expected return date), now when i try to calculate the amount of months that have expried between today, and the expected return date using DateDiff ...

retdate = Me.edate.Value
aretdate = Date
late = DateDiff("m", retdate, aretdate, vbUseSystem)
MsgBox (late)

1700972517970.png


i get -5

any suggestions on how i can fix this ??
 

Attachments

  • 1700972230722.png
    1700972230722.png
    6.2 KB · Views: 55
Try reversing the order of the two date arguments to DateDiff.

But also understand that using larger units (like months) can run into some rounding internally. You might do better to compute days and then divide by 30 (or something similar).
 
Try reversing the order of the two date arguments to DateDiff.

But also understand that using larger units (like months) can run into some rounding internally. You might do better to compute days and then divide by 30 (or something similar).
neither of those suggestion worked. instead of -5 im now getting 5 when i reverse the dates, and with the dates reversed when i changed the parameter from "m" to "d" and divided by 30 like you suggested, i still end up with 5
 
What is your regional settings?
 
"months that have expried between today, and the expected return date"
?DateDiff("m",Date(),#4/25/2024#,vbUseSystem)
This returns 5. What else do you expect?
 
"months that have expried between today, and the expected return date"
?DateDiff("m",Date(),#4/25/2024#,vbUseSystem)
This returns 5. What else do you expect?
oh snap ... i didnt even see that ... tyvm. two sets of eyes are better than one. im tired too ... thanks a million.
 
OK, when I do the following in the immediate window, this is what I get (today is 11/26/2023 in my timezone):

Code:
debug.Print datediff("m", #1/25/2023#, #4/25/2024# )
15

debug.Print datediff("m", Date(), #4/25/2024# )
5

I see no problems here. What date did you expect to see?

By the way, the parameters for DateDiff don't appear to condone "vbUseSystem" as an argument. But as it happens, vbUseSystem translates to 0, which by strange coincidence, is also the default day (Sunday). So you got away with using vbUseSystem in that context. But you could have omitted that last argument and allowed the default to take effect anyway.
 
The actual constant is vbUseSystemDayOfWeek which is also 0.
vbSunday is 1.
 

Users who are viewing this thread

Back
Top Bottom