showing results for - "javascript time difference of two mins"
Diana
22 Nov 2016
1function timeDiffCalc(dateFuture, dateNow) {
2    let diffInMilliSeconds = Math.abs(dateFuture - dateNow) / 1000;
3
4    // calculate days
5    const days = Math.floor(diffInMilliSeconds / 86400);
6    diffInMilliSeconds -= days * 86400;
7    console.log('calculated days', days);
8
9    // calculate hours
10    const hours = Math.floor(diffInMilliSeconds / 3600) % 24;
11    diffInMilliSeconds -= hours * 3600;
12    console.log('calculated hours', hours);
13
14    // calculate minutes
15    const minutes = Math.floor(diffInMilliSeconds / 60) % 60;
16    diffInMilliSeconds -= minutes * 60;
17    console.log('minutes', minutes);
18
19    let difference = '';
20    if (days > 0) {
21      difference += (days === 1) ? `${days} day, ` : `${days} days, `;
22    }
23
24    difference += (hours === 0 || hours === 1) ? `${hours} hour, ` : `${hours} hours, `;
25
26    difference += (minutes === 0 || hours === 1) ? `${minutes} minutes` : `${minutes} minutes`; 
27
28    return difference;
29  }
30
31  console.log(timeDiffCalc(new Date('2019/10/1 04:10:00'), new Date('2019/10/2 18:20:00')));
32
33// the time difference is:
34// 1 day, 14 hours, 10 minutes