获取当前日期并在周末离开

时间:2013-06-17 17:20:05

标签: javascript jquery html date

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

        });

1 个答案:

答案 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),这将执行以下操作:

  1. 今天获取
  2. 添加一天
  3. 如果新的一天是周末,它会忽略它
  4. 如果新的一天是工作日,请跟踪它是否有效
  5. 如果达到的有效天数与原始目标匹配,则会停止查看
  6. 如果达到的有效天数与原始目标不匹配,请转到#2
  7. 打印找到的日期