I have a problem with 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 get the date you send (in default format for date yyyy-mm-dd hh:mm:ss
) and calculate the difference between the first and second dates. NOTE: In the function I put so that if only one date is sent, it calculates using the current date to facilitate the service in my application ...