Copying one Datepicker to another and incrementing one year (jQuery UI)

2

In a project of mine, I have two fields set to datepicker (jQuery UI). Only the first one is editable ( InitialDate ). The second ( FinalDate ) must have the same value of InitialDate plus a year.

If I use the following code:

<script>
    $(document).ready(function () {
        $("#InitialDate").change(function () {
            var d = $.datepicker.parseDate('dd/mm/yy', $(this).val());
            d.setFullYear(d.getFullYear() + 1);
            $('#FinalDate').datepicker('setDate', d);
        });
    });
</script>

The script sums wrong. The date is five days less.

Searching, I found that I have to modify the code for something like this:

<script>
    $(document).ready(function () {
        $("#InitialDate").change(function () {
            var d = $.datepicker.parseDate('dd/mm/yy', $(this).val());
            var year = parseInt(1, 10);
            d.setFullYear(d.getFullYear() + year);
            $('#FinalDate').datepicker('setDate', d);
        });
    });
</script>

Why?

    
asked by anonymous 11.02.2014 / 15:14

1 answer

4

Your code works, apparently the problem is with the language format, its parse converts to dd/mm/yy format while datepicker displays in mm/dd/yy format.

See an example on JSFiddle

Look for the translation at en-BR of datepicker.

    
11.02.2014 / 17:03