I want to generate a random date between two dates and between two times in javascript. For instance I want to generate a random date (between 8 am and 6 pm) between today and next tomorrow. I have tried a whole bunch of things but none of them work so I won't be pasting any code since it does not work. Has anyone done something similar
function generateRandomDate(start, end) {
return new Date(start + Math.random() * (end - start));
}
The code I am using for generating random dates is posted above
I think I understand what you are after. This will return a random date between start
and end
, with a random hour between startHour
and endHour
(which should be values in the range 0-23
).
function randomDate(start, end, startHour, endHour) {
var date = new Date(+start + Math.random() * (end - start));
var hour = startHour + Math.random() * (endHour - startHour) | 0;
date.setHours(hour);
return date;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments