简体   繁体   English

使用jquery中的开始日期限制结束日期

[英]restrict end date using start date in jquery

below snippet belongs to select two dates, i have to add another feature for that ie, end date selection should be limited for one day greaterthan start date 下面的片段属于选择两个日期,我必须添加另一个功能,即结束日期选择应限制为一天更大的开始日期

$(function() {
    $( "#fromDate" ).datepicker({
        defaultDate: "+0",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat:"yy-mm-dd",
        onClose: function( selectedDate ) {
            $( "#toDate" ).datepicker( "option", "minDate", selectedDate);
            this.focus();
        }
    });
    $( "#toDate" ).datepicker({
        defaultDate: "+0",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat:"yy-mm-dd",
        onClose: function( selectedDate ) {
            $( "#fromDate" ).datepicker( "option", "maxDate", selectedDate );
        }
    });
});

thx in advance!!! thx提前!!!

I Dont know this is the correct method , but it will work as you need 我不知道这是正确的方法,但它会按你的需要工作

$(function() {
        $( "#from" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onClose: function( selectedDate ) {
                var today = new Date(selectedDate);
                var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
                var curr_date = tomorrow.getDate();
                var curr_month = tomorrow.getMonth() + 1; //Months are zero based
                var curr_year = tomorrow.getFullYear();

                var max_string = curr_month+"/"+curr_date+"/"+curr_year;

                $( "#to" ).datepicker( "option", "minDate", selectedDate );
                $( "#to" ).datepicker( "option", "maxDate", max_string );
            }
        });
        $( "#to" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onClose: function( selectedDate ) {
                $( "#from" ).datepicker( "option", "maxDate", selectedDate );
            }
        });
    });

Simple solution to your question, just the two line of js code and BINGO!!. 简单解决你的问题,只需两行js代码和BINGO !! Just change the number as per requirement (In case you want to put Max days to 1 or more) 只需根据要求更改数量(如果您想将Max天数设置为1或更多)

$("#from").datepicker({
                defaultDate: "+1w",
                changeMonth: true,
                numberOfMonths: 1,
                onClose: function (selectedDate) {
                    var d = new Date(selectedDate);
                    d.setDate(d.getDate() + 1);
                    $("#to").datepicker("option", "minDate", selectedDate);
                    $("#to").datepicker("option", "maxDate", d);
                }
            });
            $("#to").datepicker({
                defaultDate: "+1w",
                changeMonth: true,
                numberOfMonths: 1,
                onClose: function (selectedDate) {
                    $("#from").datepicker("option", "maxDate", selectedDate);
                }
            });
$(document).ready(function(){
    $("#txtFromDate").datepicker({ 
        numberOfMonths: 2,
        onSelect: function(selected) { 
            $("#txtToDate").datepicker("option","minDate", selected) 
        }
    });
    $("#txtToDate").datepicker({
        numberOfMonths: 2,
        onSelect: function(selected) { 
            $("#txtFromDate").datepicker("option","maxDate", selected)
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript / jquery中限制开始日期之前的结束日期选择 - How to restrict the selection of end date before the start date in javascript / jquery 日期时间选择器限制开始和结束日期 - Datetimepicker restrict start and end date 使用 jQuery 从开始日期和持续时间显示结束日期 - Display End Date from Start Date and Duration using jQuery Jquery FullCalendar延长开始日期或结束日期 - Jquery FullCalendar Extend Start Date or End Date 从jQuery中的开始日期开始计算结束日期 - calculating end date from start date in jquery jQuery datepicker:如何设置开始和结束日期 - jQuery datepicker: how to set start and end date jQuery验证来比较开始和结束日期 - jQuery validation to compare start and end date 我希望在jQuery r Bootstrap中将END日期中的datepicker作为当前日期并将START日期作为END日期之前7天 - I want datepicker in that END date as current date and START date as 7 days previous to END date in Jquery r Bootstrap 如何使用JavaScript或jQuery限制日期? - How to restrict date using JavaScript or jQuery? 通过Jquery Datepicker基于开始日期的结束日期在通过类调用时无法动态运行 - End date based on start date using Jquery Datepicker not working dynamically when it is called through the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM