function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Srinivas Reddy Annadi 23Srinivas Reddy Annadi 23 

Calculate the age by using DOB is more than 2 years is not working properly

When I calculate the age by using DOB and it's not working properly.If I enter date 8/14/2017 and compare age is more than 2 years and condition is not satisfying because i will devide the days with 365.2425 and it showing as result 1.998672115 but I need more than two years for the date i entered because 8/14/2019 to 8/14/2019 is more than 2 years .I calculate the above date as

 public static Boolean above2Years(Date dateOfBirth) {          
Decimal totaldays=
 dateOfBirth != null ? dateOfBirth.daysBetween(system.today()) : 0;
        return (totaldays/365.2425) > 2 ? true : false;
    }
      

Can you please help on this one
Best Answer chosen by Srinivas Reddy Annadi 23
MKRMKR
Hi,

Try this:
 
public static Boolean above2Years(Date dateOfBirth) {
    Date twoYearsBack = Date.today().addYears(-2);
    return twoYearsBack >= dateOfBirth;
}

Regards,
MKR​​​​​​​

All Answers

MKRMKR
Hi,

Try this:
 
public static Boolean above2Years(Date dateOfBirth) {
    Date twoYearsBack = Date.today().addYears(-2);
    return twoYearsBack >= dateOfBirth;
}

Regards,
MKR​​​​​​​
This was selected as the best answer
Srinivas Reddy Annadi 23Srinivas Reddy Annadi 23
Thank you very much MKR and it's working as expected.