开发者社区 问答 正文

关于日历,我想禁用上个月日期,从2019,10,31开始

My Calender code is
Calendar.setup ({
    inputField : "date11", ifFormat : "%Y-%m-%d", showsTime : false, button : "date11_img"+t_i, singleClick : true, step : 1, dateStatusFunc : dateRange
    });
Function 
function dateRange(date)
{
    var todaydate="";
    var d = new Date(),
    month = '' + (d.getMonth() + 1),
    day = '' + d.getDate(),
    year = d.getFullYear();
    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;
    todaydate= [year, month, day].join('-');
    var splittoday = todaydate.split("-");
    var m = Number(splittoday[1])-1;
    var now = new Date(Number(splittoday[0]),Number(m),Number(splittoday[2]),0,0,0);
    now.setDate(now.getDate());
    return (date.getTime() > now.getTime());
}   
I want to how to disable 2 different sets of dates . Currently i have disabled all the future dates . 
Now i want to disable all past month dates for the same calender.

应该将两组不同的值传递给这个场景处理的对象。我已经调用了一个函数,但是我想再调用一个函数。需要方法来做那件事。

展开
收起
sossssss 2019-11-28 17:08:42 568 分享 版权
阿里云 AI 助理回答

To disable both future dates and all past month dates in your calendar, you can modify the dateRange function to check for both conditions. You're already disabling future dates; to also disable all dates from previous months, you'll need to add a condition that checks if the given date is before the current month.

Here's how you can adjust your dateRange function:

function dateRange(date) {
    var d = new Date();
    var currentDate = d.getDate();
    var currentMonth = d.getMonth(); // Note: getMonth() returns 0-11, so no need to add 1 here for comparison
    var currentYear = d.getFullYear();

    // Check if the date is in the future
    if (date.getFullYear() > currentYear || 
        (date.getFullYear() === currentYear && date.getMonth() > currentMonth) ||
        (date.getFullYear() === currentYear && date.getMonth() === currentMonth && date.getDate() > currentDate)) {
        return false; // Disable future dates
    }

    // Check if the date is from a previous month
    if (date.getFullYear() < currentYear || 
        (date.getFullYear() === currentYear && date.getMonth() < currentMonth)) {
        return false; // Disable dates from previous months
    }

    // If neither condition is met, the date is valid
    return true;
}

This updated dateRange function now disables both future dates and dates from any month prior to the current one. The function first checks if the date is in the future by comparing years, then months, and finally days if necessary. It then checks if the date belongs to a previous month based on the year and month alone. If either condition is met, it returns false, effectively disabling those dates in your calendar.

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答地址: