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 :
Set variable module : give it my specific date (21/07/2024)
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.
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:
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:
Input: The function calculateAge takes a single argument birthDateString, which is a string representing the birthdate (e.g., “YYYY-MM-DD”).
Date Conversion: It converts the birthdate string into a Date object.
Current Date: It gets the current date using new Date().
Year Difference: It calculates the age by subtracting the birth year from the current year.
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.
Return Age: Finally, it returns the calculated age.
Hi there! To calculate age accurately, use (specific date - birthdate) / (1000*60*60*24*365.25) to get years or you can use online calculators. Format the result and place it in your Google Sheet.