Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant method to generate array of random dates within two dates

I have a datepicker where I show two months and I want to randomly choose 3 dates in each visible month

  $('.date').datepicker({     minDate: new Date(),     dateFormat: 'DD, MM, d, yy',     constrainInput: true,     beforeShowDay: processDates,     numberOfMonths: 2,     showButtonPanel: true,     showOn: "button",     buttonImage: "images/calendar_icon.jpg",     buttonImageOnly: true       }); 

Here is my calculation

var now = new Date(); var nowTime = parseInt(now.getTime()/1000); var randomDateSet = {};  function getRandomSet(y,m) {   var monthIndex = "m"+y+""+m; // m20121 for Jan   if (randomDateSet[monthIndex]) return randomDateSet[monthIndex];   // generate here . . - I need this part .   return randomDateSet[monthIndex]; }  function processDay(date) { // this is calculated for each day so we need a singleton for the array   var dateTime = parseInt(date.getTime()/1000);   if (dateTime <= (nowTime-86400)) {     return [false]; // earlier than today   }   var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();   var randomDates = getRandomSet(y,m);    for (i = 0; i < randomDates.length; i++) {     if($.inArray((m+1) + '-' + d + '-' + y,randomDates) != -1 || new Date() > date) {       return [true,"highlight","Some message"];     }   }   return [true,"normal"]; // ordinary day } 
like image 315
mplungjan Avatar asked Jan 27 '12 15:01

mplungjan


People also ask

How do I create a random date between two dates in Excel?

To generate random dates between two dates, you can use the RANDBETWEEN function, together with the DATE function. This formula is then copied down from B5 to B11. The result is random dates between Jan 1, 2016 and Dec 31, 2016 (random dates in the year 2016).

How do pandas generate random dates?

randn + to_timedelta. This addresses Case (1). You can do this by generating a random array of timedelta objects and adding them to your start date. This will generate dates with a time component as well.


1 Answers

Maybe I am missing something, but isn't this it?

function randomDate(start, end) {   return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())); }  const d = randomDate(new Date(2012, 0, 1), new Date()); console.log(d);
like image 200
Tomasz Nurkiewicz Avatar answered Oct 21 '22 04:10

Tomasz Nurkiewicz