For leap year calculations in your case, here is how I would do it (it would not surprise me at all to see somebody post a more direct route)
To handle all the different scenarios, it makes a complicated looking series of nested IIF statements.
The basis I use for this determination is the premise that the year number for all leap years is evenly divisible by 4.
All other years are not evenly divisible by 4.
Example:
2007/4= 501.75
2008/4= 502 (LEAP YEAR)
2009/4=502.25
2010/3=502.5
2011/4=502.75
2012/4=503 (LEAP YEAR)
Etc etc
The method I use to ascertain this uses the Int function which strips a result down to just the integer portion of a result.
OK, first determine if you're currently in a leap year as follows
IIF ( ( year ( origin_date ) ) / 4 = int ( ( year ( origin_date ) ) / 4 ), TRUE (LEAP YEAR), FALSE( NOT LEAP YEAR))
Now we’ll deal with what to do if the current year is a leap year.
You’ll need to ascertain if the origin_date is effected by the extra leap day.
Essentially, any date that is 2/29 or earlier is effected by it and any date after 2/29 is not affected by it.
So, in the “TRUE (LEAP YEAR)” section of the of the original IIF above
IIF ( month ( origin_date ) = 1, origin_date + 366, IIF (month ( origin_date ) = 2, TRUE (FEBRUARY), origin_date + 365 ) )
You’ll also need to decide what to do if the origin date is in fact the leap date. Essentially, you’ll need to either make the next year 2/28 or 3/1.
For purposes of this exercise, we’ll say you want to treat any leap date event as if it were really 2/28
So, in the “TRUE (FEBRUARY)” section of the second IIF above, you need to first test if the month of the origin_date is either January or February and then if it is the leap day.
IIF ( day ( origin_date ) = 29, origin_date + 364, origin_date + 366 )
This finishes the scenarios where the year of origin_date is a leap year as follows:
366 is added to any origin_date EARLIER than 2/29
364 is added to origin_date if origin date is 2/29
365 is added to all other origin_date values
Next, if the year of the origin_date is not a leap year you’ll need to ascertain if the year AFTER origin_date is a leap year.
So, in the section of the original IIF at the top where it says “FALSE( NOT LEAP YEAR)” you’ll need to test with another IIF similar to the first.
IIF ( ( year ( origin_date ) +1 ) / 4 = int ( ( year ( origin_date ) + 1 ) / 4 ), TRUE (NEXT LEAP YEAR), origin_date + 365 )
Now in section of that says “FALSE( NOT LEAP YEAR)” you need to ascertain if the if the origin_date + 1 year is effected by the extra leap year day. Essentially, any date for the origin_date which is LATER than 2/28 is effected, and any other date (2/28 or earlier) is not effected.
This is similar to the method for if the year of origin_date IS a leap year, only the exact opposite (except that you don’t need to check if origin_date is the leap day).
IIF ( month ( origin_date ) <=2 , origin_date + 365, origin_date + 366 ) )
This finishes the scenarios where the year following origin_date is a leap year as follows:
366 is added to any origin_date later than February
365 is added to all other origin_date values.
So the whole nested mess would look like this:
IIF(( year(origin_date))/4=int((year(origin_date))/4), IIF(month(origin_date)=1, origin_date + 366, IIF(month(origin_date )=2, IIF(day(origin_date)=29, origin_date+364,origin_date+366), origin_date+365)), IIF((year(origin_date)+1)/4=int((year(origin_date)+1)/4), IIF(month(origin_date) <=2, origin_date+365, origin_date + 366)), origin_date+365))
I could have just written it all out, but I think you should know that all the scenarios were taken into account and hopefully help you understand the method I use to account for different scenarios.
Once again, it would not surprise me for somebody else to provide a more direct method, but this is what I’ve used successfully in several apps.
I hope this helps!