0

我需要获取当前日期,然后做两件事。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);

        });
4

1 回答 1

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()]);
}

演示:http: //jsfiddle.net/TN2rN/1/

对于离开的每一天(5 天和 10 天),执行以下操作:

  1. 今天得到
  2. 增加一天
  3. 如果新的一天是周末,它会忽略它
  4. 如果新的一天是工作日,则跟踪它是否有效
  5. 如果达到的有效天数与原始目标匹配,则停止查找
  6. 如果达到的有效天数与原始目标不匹配,则转到 #2
  7. 打印找到的日期
于 2013-06-17T17:58:21.897 回答