This converts your dates to timestamps, then generates random numbers in-between and converts those back to dates. Note: added timezone
var start = new Date('Tue Jul 07 2020 10:58:05 GMT-0400').getTime(); var end = new Date('Wed Jul 08 2020 10:58:05 GMT-0400').getTime(); var dates = Array(30).fill().map(() => { return new Date( Math.floor(Math.random() * (end - start)) + start ).toString(); });
Results in:
["Wed Jul 08 2020 01:55:53 GMT-0400 (Eastern Daylight Time)" "Tue Jul 07 2020 16:58:52 GMT-0400 (Eastern Daylight Time)" "Wed Jul 08 2020 00:02:45 GMT-0400 (Eastern Daylight Time)" "Tue Jul 07 2020 15:33:55 GMT-0400 (Eastern Daylight Time)" "Tue Jul 07 2020 20:16:20 GMT-0400 (Eastern Daylight Time)" "Tue Jul 07 2020 15:25:33 GMT-0400 (Eastern Daylight Time)" "Tue Jul 07 2020 17:15:14 GMT-0400 (Eastern Daylight Time)" "Wed Jul 08 2020 02:20:32 GMT-0400 (Eastern Daylight Time)" "Tue Jul 07 2020 23:25:54 GMT-0400 (Eastern Daylight Time)" ...]
Edit: for unique dates you can do something like this:
var start = new Date('Tue Jul 07 2020 10:58:05 GMT-0400').getTime(); var end = new Date('Wed Jul 08 2020 10:58:05 GMT-0400').getTime(); var dates = []; while(dates.length < 30) { let date = new Date( Math.floor(Math.random() * (end - start)) + start ).toString(); if (!dates.includes(date)) { dates.push(date); } }
I wouldn't use this if the start and end dates are very close (within seconds of eachother) and/or the output array is very large. It will block thread.
let startDate = new Date(range.start). Then for getting the minimum number use something likestartDate.getTime().