php jquery的日期选择器日期和日期逻辑

0sgqnhkj  于 2023-08-02  发布在  PHP
关注(0)|答案(4)|浏览(113)

在from_date的on _change事件上,我想获取输入days和from_date的值,以添加days和from_date的值并将其设置为to_date
例如input_days=5 and from_date=10/02/2020,它应该在to_date中添加并自动显示15/02/2020....
下面是添加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("");
    }
});

字符串

beq87vna

beq87vna1#

请参考下面的代码。也请在评论中找到小提琴链接。如果输入为空,则默认添加5天。

<label class="required">Days</label> <input type="text" id="days"><br/><br/><br/>
<label class="required">from</label>                                                                                                           
<input type="text" id="fromDate" class="form-control date-picker from input-append minDate" placeholder="mm/yyyy"><br/><br/><br/>

<label> To </label>                                             
<input type="text" id="toDate" class="form-control date-picker to input-append maxDate" placeholder="mm/yyyy" >

$(function() {
    $( ".from" ).datepicker({
      onSelect: function( selectedDate ) {
        $( ".to" ).datepicker( "option", "minDate", selectedDate );
        var toDate = $('.from').datepicker('getDate');
        var days = $("#days").val()  != "" ? parseInt($("#days").val()) : 5;
                toDate.setDate(toDate.getDate() + days );
                $('.to').datepicker('setDate', toDate);
      }
    });
    $( ".to" ).datepicker({
      onSelect: function( selectedDate ) {
        $( ".from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });

字符串

2izufjch

2izufjch2#

试试这个
public function(){

$('#txtFromDate').datepicker({
        format: 'dd/mm/yyyy',
        startDate: 'd',
        minDate: new Date('today'),
        language: locale,
        autoclose: true,
        todayHighlight: true
    });
    $('#txtToDate').datepicker({
        format: 'dd/mm/yyyy',
        startDate: '+2d',/change value for to 5 for 5 days
        minDate: '#txtToDate',
        viewMode: 'years',
        language: locale,
        autoclose: true,
    });

字符串

dl5txlt9

dl5txlt93#

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

字符串

jm2pwxwz

jm2pwxwz4#

试试这个:

$(document).ready(function() {
    jQuery("#from").datepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
        onClose: function( selectedDate ) {
        jQuery( "#to" ).datepicker( "option", "minDate", selectedDate );
        }
    });
    jQuery("#to").datepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
        onClose: function( selectedDate ) {
        jQuery( "#from" ).datepicker( "option", "maxDate", selectedDate );
        }
    });
});

个字符

相关问题