如何找到即将到来的工作日?

时间:2014-04-04 20:36:48

标签: twitter-bootstrap datepicker bootstrap-datepicker

如何将Bootstrap datepicker endDate设置为下周的星期一,所以每次打开日历时,无论今天的哪一天都是如此,它应该设置endDate到下一个星期一和那天之后应该禁用所有日期吗?

我试图使用endDate:“+ 1w”但是它在7天后禁用。

$(".date-picker").datepicker({
    autoclose: true,
    startDate: "01/01/2014",
    endDate: "+1w",
    format: "dd M yyyy"
});

2 个答案:

答案 0 :(得分:0)

我自己解决了问题:

var current_day = new Date();
    if(current_day.getDay() == 0){
        $(".date-picker").datepicker({
            autoclose: true,
            startDate: "01/01/2014",
            endDate: "+0d",
            format:    "dd M yyyy"
        });
    }else if(current_day.getDay() == 1){
        $(".date-picker").datepicker({
            autoclose: true,
            startDate: "01/01/2014",
            endDate: "+6d",
            format:    "dd M yyyy"
        });
    }else if(current_day.getDay() == 2){
        $(".date-picker").datepicker({
            autoclose: true,
            startDate: "01/01/2014",
            endDate: "+5d",
            format:    "dd M yyyy"
        });
    }else if(current_day.getDay() == 3){
        $(".date-picker").datepicker({
            autoclose: true,
            startDate: "01/01/2014",
            endDate: "+4d",
            format:    "dd M yyyy"
        });
    }else if(current_day.getDay() == 4){
        $(".date-picker").datepicker({
            autoclose: true,
            startDate: "01/01/2014",
            endDate: "+3d",
            format:    "dd M yyyy"
        });
    }else if(current_day.getDay() == 5){
        $(".date-picker").datepicker({
            autoclose: true,
            startDate: "01/01/2014",
            endDate: "+2d",
            format:    "dd M yyyy"
        });
    }else{
        $(".date-picker").datepicker({
            autoclose: true,
            startDate: "01/01/2014",
            endDate: "+1d",
            format:    "dd M yyyy"
        });
    }

答案 1 :(得分:0)

  

下周一到来

你的问题是一种有趣的问题。所以我试图解决,但我无法理清一个简单的逻辑。我的意思是在C#中遇到类似的问题 "Datetime - Get next tuesday"

@Jon Skeet已经为下一周找出了一个简单的逻辑。

//consider date as April 1, 2014 (TUESDAY)
var daysUntilMonday = (1 - date.getDay() + 7) % 7;

<强>解释

  

这里1(表示由于天数的星期一被视为数组)

     

//(... + 7)%7确保我们得到的范围为[0,   6](作为Jon引用)


现在只需添加那些日期并将其设置回日期,如

date.setDate(date.getDate() + daysUntilMonday );

完整代码:

  $('#datepicker').datepicker({
      autoclose: true,
      startDate: "01/04/2014",
      endDate: nextMonday("01/04/2014"),
      format: "dd M yyyy"
  });

  function nextMonday(theDate) {
      var dat = theDate.split("/");
      var date = new Date(dat[2], (+dat[1]) - 1, dat[0]);
      var daysUntilMonday = (1 - date.getDay() + 7) % 7;
      date.setDate(date.getDate() + daysUntilMonday);
      return date;
  }

JSFiddle

<强>供参考:  如果日期已经是星期一会发生什么?检查..