2

如何将 Bootstrap 日期选择器设置endDate为下周的星期一,所以每次打开日历时,无论今天是星期几,都应该设置endDate为下一个星期一,并且在那一天之后应该禁用所有日期?

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

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

2 回答 2

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

仅供参考: 如果日期已经是星期一会怎样?核实..

于 2014-04-04T22:11:45.460 回答
0

I solved problem on my own:

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"
        });
    }
于 2014-04-04T21:31:57.283 回答