jQuery UI datepicker maxdate

走远了吗. 提交于 2019-12-10 11:08:39

问题


I am in need of having the maxDate for my jQueryUI date picker dynamically update depending on the day. Right now, as you will see below, there is a function being used to disable Monday-Thursday because our team can only report issues Friday-Sunday. So what I need to do is have it so that the team can continue to report on the weekend but not for the next weekend until Thursday. For example, today is Monday, the team can still report on issues from last Thursday-Sunday and won't be able to select a day for next weekend until Thursday. Is this possible?

Here is the code I have so far:

  var fullDate = new Date();
  var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);      

  $('#startdate').datepicker({
    maxDate: twoDigitMonth + "/" + fullDate.getDate() + "/" + fullDate.getFullYear(),
    beforeShowDay: disableSpecificWeekDays
  });


  function disableSpecificWeekDays(date) {
    if((date.getDay()==0) || (date.getDay() == 5) || (date.getDay() == 6) ){
        return [true];
    }else{
        return [false];
    }
  }

回答1:


Something like this should work for you:

function getMaxDate() {
    // Maximum date is today by default.
    var maxDate = new Date();

    // Is today Thursday or greater (Thurs. - Sat.)
    if (maxDate.getDay() >= 4) {
        // Yes? Make the date today's date plus the difference between today and
        // next Sunday
        maxDate.setDate(
            maxDate.getDate() + (7 - maxDate.getDay()));
    }
    return maxDate;
}

$('#startdate').datepicker({
    maxDate: getMaxDate(),
    beforeShowDay: disableSpecificWeekDays
});

Example: http://jsfiddle.net/RxQB4/

Basically, write a function that encapsulates your logic. When initializing the datepicker, call that function to retrieve the maximum date.



来源:https://stackoverflow.com/questions/7395771/jquery-ui-datepicker-maxdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!