Monday, January 7, 2013

Next Birthday formula field that respects Leap Day

The following formula is calculated from a custom Birthday field and will display this year's Birthday if it has not passed, next year's Birthday if it has passed, and takes Birthdays on Leap day into consideration.


IF ( MONTH (Birthday__c)=2 && DAY (Birthday__c)=29,
    IF ((DATE (YEAR(TODAY()),1,1) + 59)  > TODAY(),
         (DATE (YEAR(TODAY()),1,1)) + 59,
         (DATE (YEAR(TODAY())+1,1,1)) + 59),
    IF (DATE (YEAR(TODAY()),MONTH(Birthday__c),DAY(Birthday__c)) > TODAY(),
         DATE (YEAR(TODAY()),MONTH(Birthday__c),DAY(Birthday__c)),
         DATE (YEAR(TODAY())+1,MONTH(Birthday__c),DAY(Birthday__c))
       )
)
 
This says that:
 
1.        if the B-Day is a leap day
a.       then if TODAY() is less than the 60th day of the year (which is 2/29 in a leap year and 3/1 in a non leap year)
i.      then display Jan 1st + 59 (the 60th day) of this year
b.      else
i.      display Jan 1st + 59 of next year
2.       else
a.       then if TODAY is less than the custom date field’s month and day of this year
i.      then display the custom month and day of this year
b.      else
 i.      display the custom month and day of next year

No comments:

Post a Comment