Elegant method to generate array of random dates within two dates

前端 未结 4 1842
夕颜
夕颜 2020-11-30 21:17

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(),         


        
4条回答
  •  执念已碎
    2020-11-30 21:32

    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()));
    }
    
    randomDate(new Date(2012, 0, 1), new Date())
    

提交回复
热议问题