Hello friends!
I would like to learn how can I generate a random Date Of Birth for 18 years and above person as test data needed for registering a user as a test scenario.
How I found the problem:
Iâve already tried:
Hello friends!
I would like to learn how can I generate a random Date Of Birth for 18 years and above person as test data needed for registering a user as a test scenario.
How I found the problem:
Iâve already tried:
It depends on how sophisticated you want. hereâs something I put together quickly. it doesnât account for leap year babies but it does account for months with 31 vs 30 days and accounts for someone not having a birthday yet this year including this month to make sure they are over 18. This is capped to an age of 99.
let randomMonth = Math.floor(Math.random() * 12) + 1;
// max age 99 (81+18)
let randomAge = Math.floor(Math.random() * 81) + 18;
let today = new Date();
var randomDay;
var randomYear;
switch (randomMonth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
randomDay = Math.floor(Math.random() * 31) + 1;
console.log(â31 day monthâ);
break;
case 4:
case 6:
case 9:
case 11:
randomDay = Math.floor(Math.random() * 30) + 1;
console.log(â30 day monthâ);
break;
case 2:
randomDay = Math.floor(Math.random() * 28) + 1;
//doesnât account for leap year
console.log(â28 day monthâ);
break;
}
if ((today.getMonth() + 1) > randomMonth) {
console.log(âalready had birthday this yearâ);
randomYear = today.getFullYear() - randomAge;
} else if ((today.getMonth() + 1) == randomMonth) {
if (today.getDate() > randomDay) {
console.log(âhavent had birthday yet this yearâ);
randomYear = today.getFullYear() - randomAge - 1;
} else {
console.log(âalready had birthday this month or today is your birthdayâ);
randomYear = today.getFullYear() - randomAge;
}
} else {
console.log(âhavent had birthday yet this yearâ);
randomYear = today.getFullYear() - randomAge - 1;
}
// month in date constructor is 0-11
let randomDob = new Date(randomYear,randomMonth - 1,randomDay,0,0,0,0);
console.log("randomAge: " + randomAge);
console.log("randomMonth: " + randomMonth);
console.log("randomDay: " + randomDay);
console.log("randomYear: " + randomYear);
console.log("randomDOB: " + randomDob.toISOString());
You can use Random Date method to generate them. You can directly use Random Date or format them using moment to get the required format. Below one dynamically Subtracts 18 years from today and generate DOB upto 100 Years. You can playaround with this.
var moment = require('moment');
let startYear= moment().subtract(18, 'years').format("YYYY");
let maxYear=moment().subtract(100, 'years').format('YYYY');
let age = randomDate(new Date(startYear, 0, 1), new Date(maxYear, 0, 1));
console.log('Age :'+age);
youâre right. i was overthinking it. going straight to the date constructor using Math.random() as the offset is much more efficient.
function randomDate(start, end, startHour, endHour) {
var date = new Date(+start + Math.random() * (end - start));
var hour = startHour + Math.random() * (endHour - startHour) | 0;
date.setHours(hour);
return date;
}
Wow - what an efficient solutionâŠthanks you so much for it. Really appreciate your help!!! @kaliswarannachimuthu
If you are generating random users, you could use faker.
URL: https://fakerapi.it/api/v1/persons?_quantity=1&_birthday_end={{DOB}}
and pre-request script:
var moment = require(âmomentâ);
pm.variables.set(âDOBâ, moment().subtract(18, âyearsâ).format((âYYYY-MM-DDâ)));