jQuery UI日期选择器maxdate [英] jQuery UI datepicker maxdate

查看:97
本文介绍了jQuery UI日期选择器maxdate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让jQueryUI日期选择器的maxDate根据日期动态更新.现在,正如您将在下面看到的那样,由于周一至周四我们的团队只能报告周五至周日的问题,因此有一个功能被用于禁用周一至周四.因此,我需要做的是使团队可以继续在周末进行报告,但直到下周四才可以在下一个周末进行报告.例如,今天是星期一,团队仍然可以报告上周四至周日的问题,并且直到下周四都无法选择下一个周末的日期.这可能吗?

这是我到目前为止的代码:

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

解决方案

类似的方法应该对您有用:

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

示例: http://jsfiddle.net/RxQB4/

基本上,编写一个封装您的逻辑的函数.初始化日期选择器时,调用该函数以检索最大日期.

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

解决方案

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.

这篇关于jQuery UI日期选择器maxdate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆