-1

I'm trying to use following function (taken from an another answer) to get a random time that's > from now and within a hourly range.

I don't know exact value I need to put into startHour

My time is 4 PM EST US and I put 16 there and 5 to the hourRange but it gives me values that's less than that.

function randomTime(rangeOfDays = 0, startHour, hourRange){
    var today = new Date(Date.now());
    return new Date(today.getYear()+1900,today.getMonth(), today.getDate()+Math.random() *rangeOfDays, Math.random()*hourRange + startHour, Math.random()*60)
}

Link to that answer, Answer

jeffbRTC
  • 1,941
  • 10
  • 29

1 Answers1

2

Here this deals with the problem by working with Ticks only. Which is easier to manage.

const getRandomTime = (offsetHour = 0, rangeInHours = 2) => {
  const curTicks = Date.now() + 0 * (offsetHour*60*60*1000);
  const addTicks = (Math.random()*rangeInHours)*60*60*1000;
  
  return new Date(curTicks + addTicks);

};

console.log((new Date()));
for(let i=0; i<10;i++) {
  console.log( getRandomTime(1, 2+i));
}
Bibberty
  • 4,670
  • 2
  • 8
  • 23