Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

generate random date in a range

function getRandomDate(startDate, endDate) {
    const minValue = startDate.getTime();
    const maxValue = endDate.getTime();
    const timestamp = Math.floor(Math.random() * (maxValue - minValue + 1) + minValue);
    return new Date(timestamp);
}

console.log(getRandomDate(new Date(2020,0,1), new Date(2029,11,31))); // Thu Jun 17 2027 23:13:22 GMT+0200
Source by javascriptf1.com #
 
PREVIOUS NEXT
Tagged: #generate #random #date #range
ADD COMMENT
Topic
Name
1+2 =