Filter for kids turning 12 years old this year? Javascript?

I’m making a youth sports team app, and I need to be able to filter out all kids turning 12 this calendar year. I’ve tried a few different If-Then-Else parameters, but I’m struggling to filter those kids with birthdays mid year and early in the year.

For data I have birth dates, and I’m hoping to have a Javascript or If-Then-Else column to help me filter the kids. Any ideas?

Just the ones turning 12 or those kids and younger?

Filter out kids turning 12 this year, show all kids that are less than 12 years old and NOT turning 12 this year

Wouldn’t all 11-year-olds be turning 12 this year? So you want to show anyone who is (or younger than) 10 or just turned 11? Hide everyone who is older (12+)?

Something like that. For instance, if a kid turns 11 this year on January 1, then they will be 11 years old all year, but only turning 12 next year. So I can’t filter that kid out. So I can filter out anyone who is 12 years old, that’s the easy filter. I can keep anyone who is 10 years old or younger. But what do I do to filter the 11 year olds that turned 11 this year vs turned 11 last year and thus will turn 12 this year?

I asked ChatGPT for a Javascript solution and this is what it gave me (but I have no idea how to intergrate this into my Glide Sheet):

// Function to check if a birth date will result in turning 12 this year
function willTurn12(birthdate) {
const currentYear = new Date().getFullYear();
const birthYear = birthdate.getFullYear();
const age = currentYear - birthYear;

return age === 11; // Age 11 corresponds to turning 12 this year
}

// Example birth date (replace this with the actual birth date you want to check)
const exampleBirthdate = new Date(‘2011-03-15’);

// Check if the example birth date will result in turning 12 this year
if (willTurn12(exampleBirthdate)) {
console.log(‘The person with the given birth date will turn 12 this year.’);
} else {
console.log(‘The person with the given birth date will not turn 12 this year.’);
}

So according to this, if I can find a way to produce the current calendar year then I can subtract the calendar year of the kids birth date. Then if I get 12, then filter out all those kids. So now I just need to figure out how to get a column that displays the calendar year

I think I figured it out.

Create math column with YEAR(N) where N is “now” and then another math column where Year(NOW)-Birth Date Year

Then if that value is greater than or equal to 12 then false

That’s what I was going to propose. Glad you found it first!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.