1

I have start and end properties in range object. And each property has time value.

range = {
  start: Tue Jul 07 2020 10:58:05,
  end: Wed Jul 08 2020 10:58:05
}

then I have to make an array of length 30 that contains random time between range.start and range.end.

[Tue Jul 07 2020 11:00:05, Tue Jul 07 2020 13:49:12, Tue Jul 07 2020 15:22:54... Wed Jul 08 2020 12:51:05, Wed Jul 08 2020 15:24:13]

I think I can do it using new Array(30).fill(dates) but have no clue what to put it inside dates.

GoonGamja
  • 1,936
  • 5
  • 20
  • 46
  • Look at the answers for https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range? and consider something like `let startDate = new Date(range.start)`. Then for getting the minimum number use something like `startDate.getTime()`. – mlibby Jul 08 '20 at 02:19

3 Answers3

2

This converts your dates to timestamps, then generates random numbers in-between and converts those back to dates. Note: added timezone

var start = new Date('Tue Jul 07 2020 10:58:05 GMT-0400').getTime();
var end = new Date('Wed Jul 08 2020 10:58:05 GMT-0400').getTime();

var dates = Array(30).fill().map(() => {
  return new Date(
    Math.floor(Math.random() * (end - start)) + start
  ).toString();
});

Results in:

["Wed Jul 08 2020 01:55:53 GMT-0400 (Eastern Daylight Time)"
"Tue Jul 07 2020 16:58:52 GMT-0400 (Eastern Daylight Time)"
"Wed Jul 08 2020 00:02:45 GMT-0400 (Eastern Daylight Time)"​
"Tue Jul 07 2020 15:33:55 GMT-0400 (Eastern Daylight Time)"
"Tue Jul 07 2020 20:16:20 GMT-0400 (Eastern Daylight Time)"
"Tue Jul 07 2020 15:25:33 GMT-0400 (Eastern Daylight Time)"​
"Tue Jul 07 2020 17:15:14 GMT-0400 (Eastern Daylight Time)"
"Wed Jul 08 2020 02:20:32 GMT-0400 (Eastern Daylight Time)"
"Tue Jul 07 2020 23:25:54 GMT-0400 (Eastern Daylight Time)"
...]

Edit: for unique dates you can do something like this:

var start = new Date('Tue Jul 07 2020 10:58:05 GMT-0400').getTime();
var end = new Date('Wed Jul 08 2020 10:58:05 GMT-0400').getTime();

var dates = [];
while(dates.length < 30) {
  let date = new Date(
    Math.floor(Math.random() * (end - start)) + start
  ).toString();
    
  if (!dates.includes(date)) {
    dates.push(date);
  }
}

I wouldn't use this if the start and end dates are very close (within seconds of eachother) and/or the output array is very large. It will block thread.

GitGitBoom
  • 1,822
  • 4
  • 5
1

I'm not big fan of Array(n).fill() just to create an empty array with n undefined elements as the basis for a loop when the array is then discarded.

A for loop is likely less code and more efficient, e.g. (same algorithm as GitGitBoom):

let range = {
  start: new Date(2020,6,7,10,58,05),
  end: new Date(2020,6,8,10,58,05)
}
let result = [];
for (let s = +range.start, diff = range.end - s, i = 30; i; --i) {
  result.push(new Date(s + Math.random() * diff).toString());
}

console.log(result.sort());

If you're prepared to use var, the result array can be declared in the for loop initialiser too.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

Assuming you have a randomTimeGenerator, function:

var randomTimes = [];
var i=30; while(i--){randomTimes.push(randomTimeGenerator(i, randomTimes))}
rexfordkelly
  • 1,623
  • 10
  • 15