Create a random Date with Javascript

Posted on October 9, 2021

Here a little snippet to create a random Date passing a range:

const randomDate = (from, to) => {
  from = from.getTime()
  to = to.getTime()
  return new Date(from + Math.random() * (to - from))
}

Here how to use it:

const d = randomDate(new Date(2010, 0, 0), new Date(2021, 0, 0)).toISOString()

// '2016-06-21T19:32:30.940Z'