aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/utils
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-09-03 00:08:53 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-09-03 00:08:53 +0300
commita65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98 (patch)
treeedf8630e9d6426124dd49854af0cb703ebc5b710 /frontend/src/utils
parentfix: revert to static homepage (#195) (diff)
downloadlphub-a65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98.tar.gz
lphub-a65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98.tar.bz2
lphub-a65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98.zip
refactor: port to typescript
Diffstat (limited to 'frontend/src/utils')
-rw-r--r--frontend/src/utils/Time.tsx42
1 files changed, 42 insertions, 0 deletions
diff --git a/frontend/src/utils/Time.tsx b/frontend/src/utils/Time.tsx
new file mode 100644
index 0000000..b83a7ed
--- /dev/null
+++ b/frontend/src/utils/Time.tsx
@@ -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