在日期选择器中添加日期时出现问题

Issue in adding dates in datepicker

本文关键字:日期 问题 添加 选择器      更新时间:2023-09-26

我有两个日期选择器绑定到两个文本框(ChkinChkout)。当我在Chkin中选择日期时,我应该在Chkout中显示Chkin+1日期。但Chkout日期在某些情况下没有正确填写。有人能告诉我哪里出了问题吗?我的代码是-

$("#Chkin").datepicker({ 
            dateFormat:  $("#Dateformat").val(),
            minDate: '+0',               
            onClose: function (dateText, inst) {
                if ($("#Dateformat").val() == "dd/mm/yy") {
                    var parts = dateText.split("/");
                    var cin = new Date(Number(parts[2]), Number(parts[1]) - 1, Number(parts[0]));
                }
                else {
                    var cin = new Date(dateText);
                }
                var cout = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+1); 
                var maxOut= new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+7);
                $("#Chkout").datepicker('option', 'minDate', cout); 
                $("#Chkout").datepicker('option', 'maxDate', maxOut);
                showDays();
            } 
        });
        var cin = new Date($("#Chkin").val());
        var cout = new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+1);   
        var maxOut= new Date(cin.getFullYear(), cin.getMonth(), cin.getDate()+7);
        $("#Chkout").datepicker({ 
            dateFormat:  $("#ctl00_ContentPlaceHolder1_hdnDateformat").val(), 
            minDate: cout, 
            maxDate: maxOut,
            onSelect: showDays });

PS:ChkinChkout值最初与一些日期绑定

这是因为当Chkin关闭时,您不会用Chkin+1设置Chkout

$("#Chkin").datepicker({})中,在调用函数showDays()之前添加$("#Chkout").datepicker( "setDate", cout );

说明:

使用当前代码,如果Chkin中的选定值小于7天或等于Chkout,则Chkout将自动设置为Chkin+1。但除此之外,Chkout将不会更改。

示例:

Chkin更改前:

Chkin = 1 August 2013
Chkout = 5 August 2013
Chkout's minDate and maxDate = 2 August 2013 and 8 August 2013

案例1——Chkin变更为2013年7月22日:

Chkout = 23 July 2013
Chkout's minDate and maxDate = 23 July 2013 and 29 July 2013

案例1解释:Chkout更改是因为旧Chkout值不在新Chkout的minDate和maxDate的范围内。

案例2-Chkin更改为2013年8月3日:

Chkout = 5 August 2013
Chkout's minDate and maxDate = 4 August 2013 and 10 August 2013

案例2的解释:Chkout仍然是一样的,因为旧的Chkout值在新Chkout的minDate和maxDate的范围内

希望这能有所帮助。