Getting duration between two dates I'm developing on a web application where I have a start and a finish date and time and have to set the duration between it.

There are inputs for dates and times.

Then, I have this function which calculates the duration between the two of them, and they are working properly. The duration, though, needs to be dynamic. So, i made this piece of code, creating a "on" on each of then, so that when it changes, they mount an object with the four datetime values and then call it to the getDuration function.

$(document.body).on('change','.data-inicio &gt; input',function () {
    var tr   = $(this).parent().parent();
    var time = {
        dstart:  dateDbToView($(this).val()),
        hstart:  tr.find('.hora-inicio').find('input').val(),
        dfinish: dateDbToView(tr.find('.data-final').find('input').val()),
        hfinish: tr.find('.hora-final').find('input').val()
    };
    var drc = getDuration(time.dstart, time.hstart, time.dfinish, time.hfinish);
    tr.find('.duracao').find('input').val(drc);
});

$(document.body).on('change','.hora-inicio &gt; input',function () {
    var tr   = $(this).parent().parent();
    var time = {
        dstart:  dateDbToView(tr.find('.data-inicio').find('input').val()),
        hstart:  $(this).val(),
        dfinish: dateDbToView(tr.find('.data-final').find('input').val()),
        hfinish: tr.find('.hora-final').find('input').val()
    };
    var drc = getDuration(time.dstart, time.hstart, time.dfinish, time.hfinish);
    tr.find('.duracao').find('input').val(drc);
});

$(document.body).on('change','.data-final &gt; input',function () {
    var tr   = $(this).parent().parent();
    var time = {
        dstart:  dateDbToView(tr.find('.data-inicio').find('input').val()),
        hstart:  tr.find('.hora-inicio').find('input').val(),
        dfinish: dateDbToView($(this).val()),
        hfinish: tr.find('.hora-final').find('input').val()
    };
    var drc = getDuration(time.dstart, time.hstart, time.dfinish, time.hfinish);
    tr.find('.duracao').find('input').val(drc);
});

$(document.body).on('change','.hora-final &gt; input',function () {
    var tr   = $(this).parent().parent();
    var time = {
        dstart:  dateDbToView(tr.find('.data-inicio').find('input').val()),
        hstart:  tr.find('.hora-inicio').find('input').val(),
        dfinish: dateDbToView(tr.find('.data-final').find('input').val()),
        hfinish: $(this).val()
    };
    var drc = getDuration(time.dstart, time.hstart, time.dfinish, time.hfinish);
    tr.find('.duracao').find('input').val(drc);
});


My question is: this looks pretty confusing for anyone but me, which can be an bad point when it comes about maintenance. There must be a better way of making this work, so, I'm asking for suggestions.
All of the change handlers are basically the same.  You could roll them all into one by using smarter jQuery selectors.

Also, the time object is pointless.  You can just use four separate variables.  I've also moved the dateDbToView() calls to declutter the tr.find(…) lines so that the parallelism is more obvious.

$('.data-inicio, .hora-inicio, .data-final, .hora-final').on('change', 'input', function() {
    var tr = $(this).parent().parent();
    var dStart = tr.find('.data-inicio &gt; input').val();
    var hStart = tr.find('.hora-inicio &gt; input').val();
    var dFinish = tr.find('.data-final &gt; input').val();
    var hFinish = tr.find('.hora-final &gt; input').val();

    tr.find('.duracao &gt; input').val(duration(
        dateDbToView(dStart),  hStart,
        dateDbToView(dFinish), hFinish
    ));
});


I suggest renaming getDuration(…) to duration(…), because it is performing a calculation, rather than retrieving information that already exists.  (For the same reason, we say Math.sin(…) rather than Math.getSin(…)).