日期选取器日期和 jquery 的日期逻辑

在from_date的 on _change 事件上,我想获取输入天数的值,from_date添加天数和from_date的值,并将其设置为to_date


例如input_days= 5和from_date = 10 / 02 / 2020,它应该添加并自动显示15 / 02 / 2020在to_date中。


这是添加from_date和to_date并显示total_date的代码,但是...我应该在这个逻辑中改变什么?


$("#fromdate,#todate").datepicker({

    minDate: 0,

    changeMonth: true,

    changeYear: true,

    firstDay: 1,

    dateFormat: 'yy/mm/dd',

});


$("#fromdate").datepicker({dateFormat: 'yy/mm/dd'});

$("#todate").datepicker({dateFormat: 'yy/mm/dd'});


$('#enddate').change(function () {

    var start = $('#fromdate').datepicker('getDate');

    var end = $('#todate').datepicker('getDate');


    if (start < end) {

        var days = (end - start) / 1000 / 60 / 60 / 24;

        $('#total_days').val(days);

    } else {


        alert("cannot select same or previous date!");

        $('#fromdate').val("");

        $('#total_days').val("");

    }

});


智慧大石
浏览 105回答 4
4回答

慕姐4208626

请参考以下代码。另外,请在评论中找到小提琴链接。如果输入为空,我将默认添加5天。<label class="required">Days</label> <input type="text" id="days"><br/><br/><br/><label class="required">from</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="text" id="fromDate" class="form-control date-picker from input-append minDate" placeholder="mm/yyyy"><br/><br/><br/><label> To </label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="text" id="toDate" class="form-control date-picker to input-append maxDate" placeholder="mm/yyyy" >$(function() {&nbsp; &nbsp; $( ".from" ).datepicker({&nbsp; &nbsp; &nbsp; onSelect: function( selectedDate ) {&nbsp; &nbsp; &nbsp; &nbsp; $( ".to" ).datepicker( "option", "minDate", selectedDate );&nbsp; &nbsp; &nbsp; &nbsp; var toDate = $('.from').datepicker('getDate');&nbsp; &nbsp; &nbsp; &nbsp; var days = $("#days").val()&nbsp; != "" ? parseInt($("#days").val()) : 5;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toDate.setDate(toDate.getDate() + days );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.to').datepicker('setDate', toDate);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; $( ".to" ).datepicker({&nbsp; &nbsp; &nbsp; onSelect: function( selectedDate ) {&nbsp; &nbsp; &nbsp; &nbsp; $( ".from" ).datepicker( "option", "maxDate", selectedDate );&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; });&nbsp;

梦里花落0921

我希望这对你有帮助.$(document).ready(function() {&nbsp; &nbsp; jQuery("#from").datepicker({&nbsp; &nbsp; &nbsp; &nbsp; dateFormat: 'dd/mm/yy',&nbsp; &nbsp; &nbsp; &nbsp; changeMonth: true,&nbsp; &nbsp; &nbsp; &nbsp; changeYear: true,&nbsp; &nbsp; &nbsp; &nbsp; onClose: function( selectedDate ) {&nbsp; &nbsp; &nbsp; &nbsp; jQuery( "#to" ).datepicker( "option", "minDate", selectedDate );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; jQuery("#to").datepicker({&nbsp; &nbsp; &nbsp; &nbsp; dateFormat: 'dd/mm/yy',&nbsp; &nbsp; &nbsp; &nbsp; changeMonth: true,&nbsp; &nbsp; &nbsp; &nbsp; changeYear: true,&nbsp; &nbsp; &nbsp; &nbsp; onClose: function( selectedDate ) {&nbsp; &nbsp; &nbsp; &nbsp; jQuery( "#from" ).datepicker( "option", "maxDate", selectedDate );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"><input type="text" id="from"><input type="text" id="to"><script&nbsp; type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script><script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

眼眸繁星

var val = $("#fromdate").val(); // your input date ID, like we have input: 5var myDate = new Date($.datepicker.formatDate('yy/mm/dd', new Date($('#fromdate').datepicker('getDate'))));var d = myDate.getDate()+parseInt(val, 10);var m =&nbsp; myDate.getMonth()+1;var y = myDate.getFullYear();$("#todate").val(new Date(yy+'/'+mm+'/'+dd));

绝地无双

试试这个$(文档).ready(函数 () {&nbsp; &nbsp; $('#txtFromDate').datepicker({&nbsp; &nbsp; &nbsp; &nbsp; format: 'dd/mm/yyyy',&nbsp; &nbsp; &nbsp; &nbsp; startDate: 'd',&nbsp; &nbsp; &nbsp; &nbsp; minDate: new Date('today'),&nbsp; &nbsp; &nbsp; &nbsp; language: locale,&nbsp; &nbsp; &nbsp; &nbsp; autoclose: true,&nbsp; &nbsp; &nbsp; &nbsp; todayHighlight: true&nbsp; &nbsp; });&nbsp; &nbsp; $('#txtToDate').datepicker({&nbsp; &nbsp; &nbsp; &nbsp; format: 'dd/mm/yyyy',&nbsp; &nbsp; &nbsp; &nbsp; startDate: '+2d',/change value for to 5 for 5 days&nbsp; &nbsp; &nbsp; &nbsp; minDate: '#txtToDate',&nbsp; &nbsp; &nbsp; &nbsp; viewMode: 'years',&nbsp; &nbsp; &nbsp; &nbsp; language: locale,&nbsp; &nbsp; &nbsp; &nbsp; autoclose: true,&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP