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.
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}}'));
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);