How to generate a random DOB for 18 years and above

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);

Thanks for the effort and reply @chrismartin97

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