1

I was looking for it sometimes but could get a straight answer. I would like to have a Random Date of Birth generated in MM-DD-YYYY format and should be older than 18 years.

I looked up here the new Dynamic variables which are generated from the faker library which generates dummy data. There are some examples of Dates but could get any help regarding the date of Birth.

Postman Dynamic variables

An excellent example I can see like generating a Random Phone number as below. I am looking for something similar for Date of Birth also.

// GENERATE RANDOM Phone Number
pm.environment.set("phone_number", pm.variables.replaceIn('{{$randomPhoneNumber}}'));
Nihir Kumar
  • 283
  • 1
  • 6
  • 19

2 Answers2

1

Thanks, @Hans for your solution. I took that and modified it a bit. Its not the Smartest way, but i am sure it does the required. By following the below steps, the YYYY of DOB returned will be always between 1990 and 1999, so that the user is always greater than 18 Years old.

//Genrate random Date of Birth in MM/DD/YY Format and the DOB always fall in between 1990 and 1999
function randomDate(start, end) {
        return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
var date = randomDate(new Date(2000, 0, 1), new Date());
var year_last_integer = Math.floor(Math.random() * 10);
var formattedDate = (date.getMonth()+1) + '/' + date.getDate() + '/' +  '199' + year_last_integer;
pm.environment.set("dob", formattedDate);
console.log("DOB: " + formattedDate);

Nihir Kumar
  • 283
  • 1
  • 6
  • 19
  • 3
    Thanks a lot for the help, I Just ran the above code in my Pre-request script, and it worked as expected. Yes, I wanted a DOB which is always more than 18 years old to bypass the validation. – Nihir Kumar Dec 21 '20 at 21:10
0

I have a Partial fix for this, I could Generate the Random DOB in the requested format MM/DD/YYYY, But i couldn't do it for 18 years and Older validation. Hopefully someone else has this solution.

//Generate random Date of Birth in MM/DD/YYYY Format
function randomDate(start, end) {
    return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
var date = randomDate(new Date(2000, 0, 1), new Date());
var formattedDate = (date.getMonth()+1) + '/' + date.getDate() + '/' + date.getFullYear();
console.log("DOB: " + formattedDate);
  • 3
    Wow. Thanks for the quick response. Yes the Partial Fix works, I could generate the DOB in MM/DD/YYYY format. However, still looking for 18 Years old validation. – Nihir Kumar Dec 20 '20 at 19:58