Generate random date between two dates and times in Javascript

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;
}

Leave a Comment