我的代码会在实际日期增加+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 :(得分:2)
测试actual_date.getDay()
,即星期几。如果5(星期五)加3天,如果6(星期六)加2天,则另外加1天。
答案 1 :(得分:2)
您可以在循环中添加一天,并检查工作日是0(=太阳)还是6(=星期六):
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
答案 2 :(得分:0)
试试:
var date = '30 Apr 2010';
var final_date = actual_date = new Date(date);
do {
final_date = new Date(final_date.getFullYear(), final_date.getMonth(), final_date.getDate()+1);
var day = final_date.getDay();
} while (day == 6 || day == 0); // (6 is saturday, 0 is sunday)
输出:
Mon May 03 2010 00:00:00 GMT+0200 (CEST)
答案 3 :(得分:0)
尝试
var date = '30 Apr 2010';
var actual_date = new Date(date);
var final_date = new Date(actual_date);
final_date.setDate(final_date.getDate() + 1 + (actual_date.getDay() > 4 ? 7 - actual_date.getDay() : 0));
演示:Fiddle
答案 4 :(得分:0)
var final_date = new Date(
actual_date.getFullYear(),
actual_date.getMonth(),
actual_date.getDay() === 5 // Friday
? actual_date.getDate()+3
: actual_date.getDay() === 6 // Saturday
? actual_date.getDate() + 2
: actual_date.getDate() + 1 // others
);
答案 5 :(得分:0)
以下示例使用momentjs
var aDate = moment("22. Apr. 2011"); // your starting date
var DAY_IN_MSEC = 1000 * 60 * 60 * 24; // a day represented in milliseconds
var fiveDays = 5 * DAY_IN_MSEC; // five days
var newDate = moment(aDate + fiveDays); // add your 5 days to original date
var dayOfWeek = newDate.weekday(); // get the index of the day *locale dependent
if (dayOfWeek >= 5) { // is saturday, sunday *locale dependent
var daysToMonday = 7 - dayOfWeek;
newDate += daysToMonday * DAY_IN_MSEC; // add the difference needded until Monday
newDate = moment(newDate); // transform the msecs in a moment object
}
alert('voila!');
答案 6 :(得分:0)
这应该有效:
var date = '30 Apr 2010';
var actual_date = new Date(date);
// Add 3 days if friday, 2 days if saturday otherwise add 1 day.
var daysToAdd = actual_date.getDay() === 5 ? 3 : actual_date.getDay() === 6 ? 2 : 1;
var final_date = new Date(actual_date.getFullYear(), actual_date.getMonth(), actual_date.getDate() + daysToAdd);
或者更详细:
var daysToAdd = 1;
if (actual_date.getDay() === 5)
daysToAdd = 3;
else if (actual_date.getDate() === 6)
daysToAdd = 2;
答案 7 :(得分:0)
使用getDay()
功能检查其是否为5(星期五)。然后添加3天。请参阅小提琴:http://jsfiddle.net/JLGxQ/
var date = '30 Apr 2010';
var actual_date = new Date(date);
if(actual_date.getDay() == 5){
x = 3;
} else {
x = 1;
}
var final_date = new Date(actual_date.getFullYear(), actual_date.getMonth(), actual_date.getDate()+x);