Calculate differences between dates for age

Hello there,

I have data coming from a cognito form. One of the answer is the birthdate.
I need to know the age of the respondants on a specific date, the 21/07/2024, and put that number in a Google sheet.

Everything else is working, but not the age calculation.

What I’ve done so far :

  1. Set variable module : give it my specific date (21/07/2024)
  2. Set variable module : try to calculate the age
    I do the specific date minus the birthdate, which gives me a number in miliseconds, and i reduce that number by dividing to get to a higher format of date (seconds, minutes, hours, days, years). All of it in the floor formula so it gives me the highest integer and not decimals.

Due to leap years, my calculation doesn’t work, the few days that are missing are messing up the calculation if the birthdate is just after the 21/07.

Is there another way to calculate the differences between dates in that case ?

Thanks a lot.

Your brackets, subtraction, and division characters are invalid. You should be selecting them from the variables panel, or manually typing the Make brackets around them like this:

{{(}}
{{)}}
{{/}}
{{-}}

samliewrequest private consultation

Join the unofficial Make Discord server to chat with us!

1 Like

Sorry, what i showed you was something i recreated fast to show you.

Here is actual trial :

  1. set variable for specific date :

  2. set variable for a birthdate

  3. Try to calculate

Here the age of the person should be 23 on that specific date but it shows 24. As I said, I think it’s because of leap years.

I think you’re right. You’ll need to special case the day of birth and consider it if it’s a leap year. If it is you’ll need to divide by 366 rather than 365.

2000 was indeed a leap year.

Find year modulo 400, if 0 then it’s a leap year

Or

If year modulo 4 is 0 and year modulo 100 is not 0 then it’s a leap year

year = int(input('Enter year: '))
if(year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)):
    print(year,'is a leap year')
else:
    print(year,'is not a leap year')

This approach disregards leap years and actually does the math based on year month and day differences.

Here’s a simple JavaScript function to calculate someone’s age based on their birthdate:

function calculateAge(birthDateString) {
    // Convert the birth date string to a Date object
    const birthDate = new Date(birthDateString);
    
    // Get today's date
    const today = new Date();
    
    // Calculate the difference in years
    let age = today.getFullYear() - birthDate.getFullYear();
    
    // Adjust the age if the birthday hasn't occurred yet this year
    const monthDifference = today.getMonth() - birthDate.getMonth();
    const dayDifference = today.getDate() - birthDate.getDate();
    
    if (monthDifference < 0 || (monthDifference === 0 && dayDifference < 0)) {
        age--;
    }
    
    return age;
}

// Example usage:
const birthDateString = "1990-06-15";
const age = calculateAge(birthDateString);
console.log(`Age is: ${age}`);

Explanation:

  1. Input: The function calculateAge takes a single argument birthDateString, which is a string representing the birthdate (e.g., “YYYY-MM-DD”).
  2. Date Conversion: It converts the birthdate string into a Date object.
  3. Current Date: It gets the current date using new Date().
  4. Year Difference: It calculates the age by subtracting the birth year from the current year.
  5. Month and Day Adjustment: To account for whether the birthday has occurred yet this year, it checks the month and day differences. If the current date is before the birthdate in the current year, it subtracts one from the age.
  6. Return Age: Finally, it returns the calculated age.
1 Like