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

Asked

Viewed 741 times

2

In a project of mine, I have two fields configured as datepicker (jQuery UI). Only the first is editable (InitialDate). The second (FinalDate) must have the same value as InitialDate plus one 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 adds wrong. The date is five days less.

Researching, I discovered 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?

  • I don’t see what’s wrong http://jsfiddle.net/bkZ4V/

  • 2

    The second code is exactly the same as the first, after all parseInt(1, 10) is always 1. What’s the difference?

1 answer

4


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

See an example on Jsfiddle

Search for the translation in en of the datepicker.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.