我试图根据给定的日期连续单击按钮来获取最近的星期三或/然后星期六的日期。我怎样才能做到这一点?$(".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>
3 回答
吃鸡游戏
TA贡献1829条经验 获得超7个赞
这样的事情应该工作
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) {
const date = new Date(dateStr);
const day = date.getDay();
const togo = day <= 3 ? 3 - day : 6 - day;
date.setDate(date.getDate() + togo);
return date;
}
添加回答
举报
0/150
提交
取消