momen.js get random date code example

Example 1: generate random date in javascript

function randomDate(start, end) {
    return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}

randomDate(new Date(2012, 0, 1), new Date())

Example 2: random dob in javascript

var dob;

//set a range of years
var min = 1900;
var max = 2004;

// Math.ceil prevents the value from being 0;
var month = Math.ceil(Math.random() * 12);
var day = Math.ceil(Math.random() * 28);
var year = Math.floor(Math.random() * (max - min) + min);

//this ensures that the format will stay mm/dd/yyyy;
if(month < 10) {
	month = "0" + month;
}
if(day < 10) {
	day = "0" + day;
}
//concatenates random dob in mm/dd/yyyy format;
dob = month + "/" + day + "/" + year;

return dob;