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

时间:2012-09-04 00:06:51

标签: javascript jquery

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

感谢。

2 个答案:

答案 0 :(得分: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);
        }
    });

答案 1 :(得分:1)

${'#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
});