gpt4 book ai didi

javascript - 日期加一天——避开周末(周六、周日)

转载 作者:行者123 更新时间:2023-11-30 10:17:53 26 4
gpt4 key购买 nike

我有一个代码可以在实际日期上加上 +1 天。

var date = '30 Apr 2010';
var actual_date = new Date(date);
var final_date = new Date(actual_date.getFullYear(), actual_date.getMonth(), actual_date.getDate()+1);

我现在想要实现的是在实际日期为星期五时避免周末(星期六和星期日)。

Normal midweek example: 
tuesday, 5th -> wednesday, 6th

Weekend example:
friday, 6th -> monday, 9th

最佳答案

您可以在循环中添加一天并检查工作日是 0 (=Sun) 还是 6 (=Sat):

d = new Date(2014, 3, 4)   // Fri Apr 04

do {
d.setDate(d.getDate() + 1)
} while(d.getDay() == 0 || d.getDay() == 6);

console.log(d); // Mon Apr 07

关于javascript - 日期加一天——避开周末(周六、周日),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22936650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com