Calculating age from date of birth in json response data

Hello everyone!
This is going to sound terribly dumb and simple to most of you no doubt, which is probably why I can’t find any similar question on it! …but forgive me I’m new!

My question:
I need to verify that the age of a customer is below or between an age range.
Example, the condition is a customer needs to be below 45 but older than 20. To verify this, all I need to do is calculate the age of the customer from subtracting the date of birth from the current date.

The response data is easily accessed:
{
“customerDob”: “25/05/1999”
}

I’ve tried various things, including using a pre-request script with moment etc, which didn’t seem to do anything. I don’t think there’s any value in writing up my code as it doesn’t work!
Any help, please, I’m sure it’s simple I’m just not getting it!

Thanks
Jes

How I found the problem:

I’ve already tried:

Hi @jes73

I didn’t get much time to play with these but here is a concept using the year to calculate age.
It could be tweaked to calculate a more specific age based on day/month (i just didn’t have time to look into it).

This would live in the “Test” tab so that it is executed after the HTTP response is received. And you will need to dynamically grab the DOB from said response.

//Get todays date
const moment= require('moment');
currentDate = moment();
//Console log the year
console.log(currentDate.format("YYYY"));

//Grab the DOB from the HTTP response (will need extra code here to grab JSON value)
let DOB = "25/05/1999";

//Remove first 6 characters to leave just the date
DOB = DOB.substring(6);
console.log(DOB);

//Age = date minus DOB
let age = currentDate.format("YYYY") - DOB;
console.log(age);

if (age <= 20)
{
    console.log("Under age");
}
else if (age > 20 && age < 45)
{
    console.log("Correct age");
}
else if (age >= 45)
{
    console.log("Over age");
}

Hope this helps, let me know if you need it refined and ill have another look when I get some time.

2 Likes

Thank you so much! I can’t wait to try this in the morning, I’ll let you know how I get on

1 Like

Thank you so much, this does exactly what I need it to do.
Grabbing the json after:
//Console log the year
console.log(currentDate.format(“YYYY”));

// grab json customer dob response
var info = pm.response.json();
pm.expect(info.customerDob).equals(“26/05/2003”);

//remove 6 chars to leave year
let dob = info.customerDob
dob = info.customerDob.substring(6);
console.log(dob);

…etc
Console returns:

“2022”
“2003”
19
“under age”

Kudos to you Sir! :slight_smile:

1 Like

I found a different way around this problem…
I needed to compare a users family profile and determine if they were 14 or older. I did it by counting backwards from today 14 years, and comparing thier DOB against that date.

let moment = require('moment');
let CurrentDate =  new Date(moment())
let DOB = response.DateOfBirth
let FourteenYearsAgo = new Date (moment())
    FourteenYearsAgo.setFullYear(FourteenYearsAgo.getFullYear()-14)

 if (FourteenYearsAgo < DOB ){
                     pm.test('Under14 test', function(){
                              pm.expect(something).to.equal(somethingElse)
})
}