Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions How to calculate someone’s age based on the current date vs a string of date

  • How to calculate someone’s age based on the current date vs a string of date

    Posted by Maeve Tan on January 4, 2023 at 5:04 pm

    So I’m having trouble figuring out this expression. I have to calculate someone’s age from a string that is the birth date(e.g 12/22/1989) based on the current date.

    On one text layer, the birthdate string will be inserted and on the other layer, the age should be outputted.

    So what I have now is this:

    var birthDateString = thisComp.layer("Birth Date");
    function dateToCurrentFormat(dateString) {
    var parts = dateString.split(/[/]/); var month = Number(parts[0]) - 1; // months are 0-based
    var day = Number(parts[1]);
    var year = Number(parts[2]);
    return new Date(year, month, day);
    }

    function difference(date1, date2, interval) {
    var second = 1000,
    minute = second * 60,
    hour = minute * 60,
    day = hour * 24,
    week = day * 7;
    date1 = new Date(date1);
    date2 = new Date(date2);
    var timediff = date2 - date1;
    if (isNaN(timediff)) return NaN;
    switch (interval) {
    case "years":
    return date2.getFullYear() - date1.getFullYear();
    case "months":
    return (
    (date2.getFullYear() * 12 + date2.getMonth()) -
    (date1.getFullYear() * 12 + date1.getMonth())
    );
    case "weeks":
    return Math.floor(timediff / week);
    case "days":
    return Math.floor(timediff / day);
    case "hours":
    return Math.floor(timediff / hour);
    case "minutes":
    return Math.floor(timediff / minute);
    case "seconds":
    return Math.floor(timediff / second);
    default:
    return undefined;
    }
    }
    var birthDate = dateToCurrentFormat(birthDateString);

    var age = difference(birthDate, new Date(), "years");

    var ageString = age + " years old";
    ageString;

    Each time I’m getting an error for different things. First, it was “dateToCurrentFormat is not defined” then, ” difference is not defined” and then “object of type found where a Number, Array, or property is needed” for the variable age.

    What am I doing wrong here? Maybe I’m doing it more difficult than it should be, if there’s an easier way to get to the result please let me know!
    TIA! 🙂

    Maeve Tan replied 3 years, 6 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    January 4, 2023 at 7:38 pm

    I found this one on stackoverflow:

    function calculate_age(birth_month,birth_day,birth_year)
    {
    today_date = new Date();
    today_year = today_date.getFullYear();
    today_month = today_date.getMonth();
    today_day = today_date.getDate();
    age = today_year - birth_year;
    if ( today_month < (birth_month - 1))
    {
    age--;
    }
    if (((birth_month - 1) == today_month) && (today_day < birth_day))
    {
    age--;
    }
    return age;
    }
  • Maeve Tan

    January 5, 2023 at 9:16 am

    Thank you I will try this out!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy