如何在每个按钮上连续单击最接近的候选日期和下一个日期

我试图根据给定的日期连续单击按钮来获取最近的星期三或/然后星期六的日期。我怎样才能做到这一点?


$(".btn-info").on("click", function(){

   GetDate('2019-09-10');

});


function GetDate(x){

  x = new Date();

  var cc = x.setDate(x.getDate() + (1 + 5 - x.getDay()) % 7);

  console.log(cc);

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<button type="button" class="btn btn-info">Get Next Wednesday / Saturday Date ++ </button>


Cats萌萌
浏览 193回答 3
3回答

吃鸡游戏

这样的事情应该工作console.log(getNext("2019-09-09"));console.log(getNext("2019-09-10"));console.log(getNext("2019-09-11"));console.log(getNext("2019-09-12"));console.log(getNext("2019-03-06"));function getNext(dateStr) {&nbsp; const date = new Date(dateStr);&nbsp; const day = date.getDay();&nbsp; const togo = day <= 3 ? 3 - day : 6 - day;&nbsp; date.setDate(date.getDate() + togo);&nbsp; return date;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript