HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Getting duration between two dates

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
datesgettingtwobetweenduration

Problem

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 > 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 > 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 > 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('.

Solution

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 > input').val();
    var hStart = tr.find('.hora-inicio > input').val();
    var dFinish = tr.find('.data-final > input').val();
    var hFinish = tr.find('.hora-final > input').val();

    tr.find('.duracao > 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(…)).

Code Snippets

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

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

Context

StackExchange Code Review Q#82114, answer score: 2

Revisions (0)

No revisions yet.