我需要获取当前日期然后做两件事。 1)加10天。 2)加5天。 我也必须离开周末。意味着当天加10,如果出现任何周末,那么我必须离开。
我怎样才能实现这个目标?
直到现在我所做的是:
$(document).ready(function() {
var d = new Date();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var n = month[d.getMonth()];
var dt = d.getDate();
$('#date_span').text(dt);
$('#month_span').text(n);
});
答案 0 :(得分:1)
您似乎想要距今天5天和10天的日子,不包括周末。你可以试试这个:
var MONTHS = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
targetDays = [5, 10],
i, j,
targetDate,
curTargetDay,
curDay,
finalMonth,
finalDate;
for (i = 0, j = targetDays.length; i < j; i++) {
targetDate = new Date();
curTargetDay = targetDays[i];
while (curTargetDay) {
targetDate.setDate(targetDate.getDate() + 1);
curDay = targetDate.getDay();
if (curDay !== 0 && curDay !== 6) {
curTargetDay--;
}
}
console.log(targetDate.getDate(), MONTHS[targetDate.getMonth()]);
}
DEMO: http://jsfiddle.net/TN2rN/1/
对于每个天数(5和10),这将执行以下操作: