Home

Published

- 1 min read

js get random data between two dates

img of js get random data between two dates

The solution for this is noted below

js get random data between two dates

Solution

   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
}

console.log(randomDate(new Date(2020, 0, 1), new Date(), 0, 24))

Try other methods by searching on the site. That is if this doesn’t work