如何根据其他日期的输入更新日期

How can I update a date based on the input from another date?

本文关键字:日期 更新 输入 其他 何根      更新时间:2023-09-26

我在一个页面中有一个表单,其中有两个接受日期的输入。如何使用jQuery根据输入#1的日期自动填充输入#2?基本上,我希望输入#2是从输入#1的日期起1年。

谢谢。

试试这个:

  $("#input1").datepicker({
        dateFormat: 'dd/mm/yy',
        onSelect: function(dateStr) {
            var d = $.datepicker.parseDate('dd/mm/yy', dateStr);
            var years = 1;
            d.setFullYear(d.getFullYear() + years);
            $('#input2').datepicker('setDate', d);
        }
    });
${'#input1').change(function(){
  date = new Date($('#input1').val()); // You may have to parse this, depending on the format of the input
  $('#input2').val(date + (365 * 24 * 60 * 60 * 1000)); // JS Date object counts in milliseconds.  You may have to format this with a few lines of code, depending on what formatting you want
});