People to solve my problem, I ended up creating a function like this:
function timeDiff(d1, d2) {
var d1 = new Date(d1).getTime();
var d2 = d2 || new Date().getTime();
var df = Math.abs(d1 - d2);
var td = {
d: Math.round(df / (24 * 60 * 60 * 1000)), //dias
h: Math.round(df / (60 * 60 * 1000)), //horas
m: Math.abs(Math.round(df / (60 * 1000)) - (60 * 1000)), //minutos
s: Math.abs(Math.round(df / 1000) - 1000)
};
var result = '';
td.d > 0 ? result += td.d + ' dias ' : '';
td.h > 0 ? result += ('0' + td.h).slice(-2) + ':' : '00:';
td.m > 0 ? result += ('0' + td.m).slice(-2) + ':' : '00:';
td.s > 0 ? result += ('0' + td.s).slice(-2) : '00';
return result;
}
This function will take the date you send (in default date format yyyy-mm-dd hh:mm:ss
) and calculate the difference between the first and the second date. NOTE: in the function I put so that if sent only one date, it calculates using the current date to facilitate the service in my application...
ok, that I should get to do the calculation. but had understood that there was a better method than this...
– LeandroLuk