diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-11-04 13:47:50 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-04 13:47:50 +0300 |
| commit | eae19bb1b047b3568e7a9a624b50e80886e56331 (patch) | |
| tree | aa3215377fd1a7714e4c5763f9abf9d7dc7def2b /frontend/src/utils/Time.ts | |
| parent | fix: remove insert trigger on users, check insert user on login err (#224) (diff) | |
| download | lphub-eae19bb1b047b3568e7a9a624b50e80886e56331.tar.gz lphub-eae19bb1b047b3568e7a9a624b50e80886e56331.tar.bz2 lphub-eae19bb1b047b3568e7a9a624b50e80886e56331.zip | |
feat/frontend: optimizing imports, file extensions (#230)
Co-authored-by: FifthWit <fifthwitbusiness@gmail.com>
Diffstat (limited to 'frontend/src/utils/Time.ts')
| -rw-r--r-- | frontend/src/utils/Time.ts | 42 |
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 @@ | |||
| 1 | export 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 | |||
| 32 | export 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 | ||