vendredi 20 février 2015

What should be the proper way of using getting duration between two dates?


guys! First of all, sorry if my english isn't THAT good, because i don't speak it as my first language. Anyway, i'm developing on a WebApplication, where i have a start and a finish date and time and have to set the duration between it.


They are made by a input type date and input type time.


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('.duracao').find('input').val(drc);
});

$(document.body).on('change','.hora-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(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, but i can't think of any right now. So, i'm asking for suggestions...


Thanks, guys!





Aucun commentaire:

Enregistrer un commentaire