Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Age calculation using DOB

Avatar

Former Community Member

Hi All,

I have a requirement to calculate the age of a person through client side scripting. I have a Date of birth field in the form.

Now basically i want to find the difference of curent date and the date of birth entered by the user.

Pls provide the script for the same.

Thanks

Abhiram

1 Reply

Avatar

Level 10

Hi.

Try this function;

function calculateAge(dateOfBirth)

{

    var today = new Date();

    var age = today.getFullYear() - dateOfBirth.getFullYear();

    var m = today.getMonth() - dateOfBirth.getMonth();

    if (m < 0 || (m === 0 && today.getDate() < dateOfBirth.getDate()))

    {

        age--;

    }

    return age;

}

To call it use;

var dateOfBirth = new Date(util.scand("yyyy-mm-dd", DateOfBirth.rawValue).setHours(0,0,0,0));

app.alert(calculateAge(dateOfBirth))

This assume your DateTime Field is called DateOfBirth, I do the setHours bit to make sure there is no time component but you probably don't need it.

Regards

Bruce