/
timer.ts
42 lines (31 loc) · 1.07 KB
/
timer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
function dateDiff(dateStr) {
if (dateStr == null)
return '';
var endDate = new Date();
var startTime = new Date(dateStr);
var time1 = endDate.getTime();
var time2 = startTime as unknown as number;
var seconds = Math.abs(time1- time2) / 1000;
// get total seconds between the times
var delta = seconds; //Math.abs(date_future - date_now) / 1000;
// calculate (and subtract) whole days
var days = Math.floor(delta / 86400);
delta -= days * 86400;
// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
// what's left is seconds
var seconds = Math.floor(delta % 60); // in theory the modulus is not required
if (days > 0)
return (days + " days");
if (hours > 0)
return (hours + " hours");
if (minutes > 0)
return (minutes + " minutes");
return (seconds + " seconds");
}
export { dateDiff }