aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/utils/Time.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/utils/Time.ts')
-rw-r--r--frontend/src/utils/Time.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/frontend/src/utils/Time.ts b/frontend/src/utils/Time.ts
new file mode 100644
index 0000000..b83a7ed
--- /dev/null
+++ b/frontend/src/utils/Time.ts
@@ -0,0 +1,42 @@
1export function time_ago(date: any) {
2 const now = new Date().getTime();
3
4 const localDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60000));
5 const seconds = Math.floor((now - localDate.getTime()) / 1000);
6
7 let interval = Math.floor(seconds / 31536000);
8 if (interval === 1) {return interval + ' year ago';}
9 if (interval > 1) {return interval + ' years ago';}
10
11 interval = Math.floor(seconds / 2592000);
12 if (interval === 1) {return interval + ' month ago';}
13 if (interval > 1) {return interval + ' months ago';}
14
15 interval = Math.floor(seconds / 86400);
16 if (interval === 1) {return interval + ' day ago';}
17 if (interval > 1) {return interval + ' days ago';}
18
19 interval = Math.floor(seconds / 3600);
20 if (interval === 1) {return interval + ' hour ago';}
21 if (interval > 1) {return interval + ' hours ago';}
22
23 interval = Math.floor(seconds / 60);
24 if (interval === 1) {return interval + ' minute ago';}
25 if (interval > 1) {return interval + ' minutes ago';}
26
27 if(seconds < 10) return 'just now';
28
29 return Math.floor(seconds) + ' seconds ago';
30};
31
32export function ticks_to_time(ticks: number) {
33 let seconds = Math.floor(ticks / 60)
34 let minutes = Math.floor(seconds / 60)
35 let hours = Math.floor(minutes / 60)
36
37 let milliseconds = Math.floor((ticks % 60) * 1000 / 60)
38 seconds = seconds % 60;
39 minutes = minutes % 60;
40
41 return `${hours === 0 ? "" : hours + ":"}${minutes === 0 ? "" : hours > 0 ? minutes.toString().padStart(2, '0') + ":" : (minutes + ":")}${minutes > 0 ? seconds.toString().padStart(2, '0') : seconds}.${milliseconds.toString().padStart(3, '0')}`;
42}; \ No newline at end of file