Skip to content Skip to sidebar Skip to footer

Why Do This Function Doesn't Return A Random Time That's > From Now?

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

Solution 1:

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

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

};

console.log((newDate()));
for(let i=0; i<10;i++) {
  console.log( getRandomTime(1, 2+i));
}

Post a Comment for "Why Do This Function Doesn't Return A Random Time That's > From Now?"