Using the Mod operator

aziz rasul

Active member
Local time
Today, 01:16
Joined
Jun 26, 2000
Messages
1,935
Here's the problem.

If I use DateDiff(“yyyy”,#31/12/02#,#01/01/04#) I get the value of 2, even though there has only really been a complete year gap of 1 year.

However, if I use
1 Mod DateDiff(“yyyy”,#31/12/02#,#01/01/04#), I get the answer of 1 which is what I wanted.

What I am trying to understand is, that according to online help, 1 Mod 2 should give a remainder of 0 NOT 1?

When I initially tried DateDiff(“yyyy”,#31/12/02#,#01/01/04#) Mod 1 (which is the way that I expected it to work) I got 0?

What am I missing here?
 
The Mod operator returns the remainder of a division function.
DateDiff returns an integer value.
So, anything returned by DateDiff Mod 1 will return 0.
Using x Mod 2 will tell you if a whole number is odd or even.

Perhaps you can use:

Int(DateDiff('d',#12/31/02#,#01/01/04#)/365)
OR
Int(DateDiff('m',#12/31/02#,#01/01/04#)/12)
?

HTH
 
Last edited:
In this instance, DateDiff() is just subtracting the smaller year from the larger year, i.e. 2004 - 2002 = 2.
What you are attempting to do is no different than when asked to determine someone's age, in years. Some debug window examples (using Medium dates to resolve the regional differencs):

DOB = #31-Dec-2002#
DteDate = #1-Jan-2004#
? datediff("yyyy", DOB, DteDate)
2
'Obviously incorrect. The way you resolve it is was with this boolean expression where True = -1 and 'False = 0
? dteDate < DateSerial(Year(dteDate),Month(DOB), Day(DOB))
True

Tack them together and you get:
? datediff("yyyy", DOB, DteDate) + (dteDate < DateSerial(Year(dteDate),Month(DOB), Day(DOB)))
1
 

Users who are viewing this thread

Back
Top Bottom