0
$('#date1').change(function() {
$('#date2').val( ($(this).val())+1);
  });

If I have a date on the "date1" textbox ex: "01/01/15" , the result using this code on "date2" is "01/01/151" instead of "02/01/15", what am i doing wrong ?

tonydeleon
  • 151
  • 1
  • 2
  • 15

1 Answers1

1

It may be not the clearest solution, but I think you want to achieve something like that:

$(function () {
    $('#date1').change(function () {
        var value = $(this).val(),
            split = value.split('/'),
            day = split[0],
            month = parseInt(split[1]) - 1 // months start counting from 0
            ,
            year = split[2];
        year = lpad(year);
        var date = new Date(Date.UTC(year, month, day));

        // add one day 
        date.setDate(date.getDate() + 1);

        // format final 
        var final = lpad(date.getDate()) + '/' + lpad((date.getMonth() + 1)) + '/' + date.getYear()
        $('#date2').val(final);
    });
});

// add leading zero
function lpad(n) {
    n = (n <= 9) ? "0" + n : n;
    return n;
}

http://jsfiddle.net/b5tx2sa9/3/

It's just to demonstrate you, where to look for an issue. In real live applications you should always(!) use a library, that will handle dates formatting for you, like: https://stackoverflow.com/a/7022296/2630208 or: https://stackoverflow.com/a/10119138/2630208 You'll also have to take care of users input (validate, format, replace).

Community
  • 1
  • 1
Kamil Szymański
  • 950
  • 12
  • 26