JQuery - end date less than start date

后端 未结 6 1785
一生所求
一生所求 2020-12-16 08:12

I\'m trying to fix this problem I\'m having. I have to input tags, one is for Training beginning date and other is for training ending date. What i am trying to do is create

相关标签:
6条回答
  • 2020-12-16 08:26
    $(document).ready(function(){
        $("#StartDate").datepicker({
            numberOfMonths: 2,
            onSelect: function(selected) {
              $("#EndDate").datepicker("option","minDate", selected)
            }
        });
        $("#EndDate").datepicker({ 
            numberOfMonths: 2,
            onSelect: function(selected) {
               $("#StartDate").datepicker("option","maxDate", selected)
            }
        });  
    });
    
    0 讨论(0)
  • 2020-12-16 08:26

    Date picker has an option to validate start date and end date.

    HTML

    jQuery

    $(document).ready(function() {
        jQuery("#from").datepicker({
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true,
            maxDate: '0',
            onClose: function( selectedDate ) {
            jQuery( "#to" ).datepicker( "option", "minDate", selectedDate );
            }
        });
        jQuery("#to").datepicker({
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true,
            maxDate: '0',
            onClose: function( selectedDate ) {
            jQuery( "#from" ).datepicker( "option", "maxDate", selectedDate );
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-16 08:30

    Here you have the same question with Jquery UI Datepicker jQuery UI Picking a start and end date within range based on start date And a working example:

    html

    <input type="text" id="dt1">
    <input type="text" id="dt2">
    

    js

    $(document).ready(function () {
    
        $("#dt1").datepicker({
            dateFormat: "dd-M-yy",
            minDate: 0,
            onSelect: function (date) {
                var dt2 = $('#dt2');
                var startDate = $(this).datepicker('getDate');
                var minDate = $(this).datepicker('getDate');
                dt2.datepicker('setDate', minDate);
                startDate.setDate(startDate.getDate() + 30);
                //sets dt2 maxDate to the last day of 30 days window
                dt2.datepicker('option', 'maxDate', startDate);
                dt2.datepicker('option', 'minDate', minDate);
                $(this).datepicker('option', 'minDate', minDate);
            }
        });
        $('#dt2').datepicker({
            dateFormat: "dd-M-yy"
        });
    });
    

    http://jsfiddle.net/PPSh3/7/

    Hope that help

    0 讨论(0)
  • 2020-12-16 08:33

    Just expanding off fusions answer. this extension method works using the jQuery validate plugin. It will validate dates and numbers

    jQuery.validator.addMethod("greaterThan", 
    

    function(value, element, params) {

    if (!/Invalid|NaN/.test(new Date(value))) {
        return new Date(value) > new Date($(params).val());
    }
    
    return isNaN(value) && isNaN($(params).val()) 
        || (Number(value) > Number($(params).val())); 
    },'Must be greater than {0}.');
    

    To use it:

    $("#EndDate").rules('add', { greaterThan: "#StartDate" });
    

    Or

    $("form").validate({
    rules: {
        EndDate: { greaterThan: "#StartDate" }
    }
    });
    
    0 讨论(0)
  • 2020-12-16 08:36

    Found the solution

    var startDate = new Date('01/01/2012');
    var FromEndDate = new Date();
    var ToEndDate = new Date();
    ToEndDate.setDate(ToEndDate.getDate() + 365);
    
    $('.from_date').datepicker({
    weekStart: 1,
    startDate: '01/01/2012',
    endDate: FromEndDate,
    autoclose: true
    })
    .on('changeDate', function (selected) {
            startDate = new Date(selected.date.valueOf());
            startDate.setDate(startDate.getDate(new Date(selected.date.valueOf())));
            $('.to_date').datepicker('setStartDate', startDate);
        });
    $('.to_date')
        .datepicker({
            weekStart: 1,
            startDate: startDate,
            endDate: ToEndDate,
            autoclose: true
        })
        .on('changeDate', function (selected) {
            FromEndDate = new Date(selected.date.valueOf());
            FromEndDate.setDate(FromEndDate.getDate(new Date(selected.date.valueOf())));
            $('.from_date').datepicker('setEndDate', FromEndDate);
        });
    
    0 讨论(0)
  • 2020-12-16 08:44

    JQuery - end date less than start date 100% Work And Also Dynamically Date.

    For Show Date With Today Date

    <script type="text/javascript">
    $(document).ready(function () {
    $('.bet_start').datepicker({
        autoclose: true,
        format: 'yyyy-mm-dd',
        todayHighlight: true,
        startDate: '0d'
    });
    })</script>
    

    For end date less than start date

    <script type="text/javascript">
    $(document).ready(function () {
    $('.bet_start').datepicker({
        autoclose: true,
        format: 'yyyy-mm-dd',
        todayHighlight: true,
        startDate: '0d'
    });
    var startDate = new Date('18/09/2019');
    var FromEndDate = new Date();
    var ToEndDate = new Date();
    ToEndDate.setDate(ToEndDate.getDate() + 365);
    
    $('.bet_start').datepicker({
        weekStart: 1,
        startDate: '18/09/2019',
        endDate: FromEndDate,
        autoclose: true
    })
        .on('changeDate', function (selected) {
            startDate = new Date(selected.date.valueOf());
            startDate.setDate(startDate.getDate(new Date(selected.date.valueOf())));
            $('.bet_end').datepicker('setStartDate', startDate);
        });
    $('.bet_end')
        .datepicker({
            weekStart: 1,
            startDate: startDate,
            endDate: ToEndDate,
            autoclose: true
        })
        .on('changeDate', function (selected) {
            FromEndDate = new Date(selected.date.valueOf());
            FromEndDate.setDate(FromEndDate.getDate(new Date(selected.date.valueOf())));
            $('.bet_start').datepicker('setEndDate', FromEndDate);
        });
    
    });</script>
    
    0 讨论(0)
提交回复
热议问题