JQuery -结束日期早于开始日期

yyhrrdl8  于 2022-11-03  发布在  jQuery
关注(0)|答案(6)|浏览(148)

我正在尝试解决这个问题。我必须输入标签,一个是训练开始日期,另一个是训练结束日期。我正在尝试做的是创建一个jQuery函数,防止用户选择的结束日期小于开始日期,反之亦然。
任何帮助都将不胜感激。

<div class="control-group">
    <label class="control-label">Training beggining date:</label>
    <input id="StartDate" style="margin-left: 10px;" size="16" type="text"/>
</div>
<div class="control-group">
    <label class="control-label">Training completion date:</label>
    <input id="EndDate" style="margin-left: 10px;" size="16" type="text"/>
</div>
0tdrvxhp

0tdrvxhp1#

$(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)
        }
    });  
});
7fhtutme

7fhtutme2#

下面是一个使用Jquery UI Datepicker jQuery UI Picking a start and end date within range based on start date的相同问题和工作示例:
超文本标记语言

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

日本

$(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/
希望能有所帮助

oipij1gg

oipij1gg3#

找到解决方案

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

iih3973s4#

这个扩展方法使用jQuery验证插件,它将验证日期和数字

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

使用方法:

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

或者

$("form").validate({
rules: {
    EndDate: { greaterThan: "#StartDate" }
}
});
qzlgjiam

qzlgjiam5#

日期选择器具有验证开始日期和结束日期的选项。
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 );
        }
    });
});
db2dz4w8

db2dz4w86#

JQuery -结束日期早于开始日期100%工作,同时动态确定日期。
显示日期与今天日期

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

对于早于起始日期的终止日期

<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>

相关问题