獲取某一天至今的日期數組

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>

</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
  function pickDate(opt){
    var minTime = +new Date(opt.minTime);//計算2019/5/20的時間,精確到毫秒
    var oneDay = 24 * 3600 * 1000; //計算一天的時間,精確到毫秒 86400000
    var date = [];
    var today =  +new Date(new Date().toLocaleDateString().split('/').join(','));
    var date_len = (today-minTime)/oneDay;//今天距離某一天的天數
    var now = new Date(minTime);
    
    for (var i = 0; i < date_len; i++) {
      now = [now.getFullYear(), now.getMonth()+1, now.getDate()].join('-');
      date.push(now);
      now = new Date(+new Date(now) + oneDay);
    }
    date.push(new Date().toLocaleDateString().split('/').join('-'));
    console.log(date)
  }

  pickDate({
    minTime:'2019,4,26'
  })


</script>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章