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.

1
  • Look at the answers for stackoverflow.com/questions/1527803/…? and consider something like let startDate = new Date(range.start). Then for getting the minimum number use something like startDate.getTime(). Commented Jul 8, 2020 at 2:19

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i am sure OP doesn't want duplicate times
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.

Comments

0

Assuming you have a randomTimeGenerator, function:

var randomTimes = []; var i=30; while(i--){randomTimes.push(randomTimeGenerator(i, randomTimes))} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.