I am facing a problem to Get Two Time Different Globally. I need diff time format days: Hours: Minutes: Seconds. Everything is ok but problem in hour. For this example hour should be 0 hour but it return 6 hours..
function getTimeDiff(join, lastSeen) {
let t1 = new Date(join).getTime(),
t2 = new Date(lastSeen).getTime(),
milliseconds = 0,
time = '';
if (isNaN(t1) || isNaN(t2)) return '';
if (t1 < t2) {milliseconds = t2 - t1;}
else {milliseconds = t1 - t2;}
var days = Math.floor(milliseconds / 1000 / 60 / (60 * 24));
var date_diff = new Date(milliseconds);
if (days > 0) time += days + 'd ';
console.log('join: ', join, ' - ', 'lastseen:', lastSeen);
console.log('Diff = ', milliseconds);
console.log('Standard Time : ', date_diff);
console.log('Hours: ', date_diff.getHours());
console.log('Minutes: ', date_diff.getMinutes());
console.log('Secounds: ', date_diff.getSeconds());
if (date_diff.getHours() > 0) time += date_diff.getHours() + 'h ';
if (date_diff.getMinutes() > 0) time += date_diff.getMinutes() + 'm ';
if (date_diff.getSeconds() > 0) time += date_diff.getSeconds() + 's ';
console.log('Result: ', time);
return time;
}
console.log(getTimeDiff(1589911275699, 1589911365116));
The output result:
**
1589911275699 " -- " 1589911365116
join: 1589911275699 - lastseen: 1589911365116
Diff = 89417
Standard Time : Thu Jan 01 1970 06:01:29 GMT+0600 (Bangladesh Standard Time)
Hours: 6 // Result should be 0
Minutes: 1
Secounds: 29
Result: 6h 1m 29s // 0h 1m 29s
**
Here already have some post but same problem https://stackoverflow.com/a/18103175