From eae19bb1b047b3568e7a9a624b50e80886e56331 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:47:50 +0300 Subject: feat/frontend: optimizing imports, file extensions (#230) Co-authored-by: FifthWit --- frontend/src/utils/Time.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 frontend/src/utils/Time.ts (limited to 'frontend/src/utils/Time.ts') 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 @@ +export function time_ago(date: any) { + const now = new Date().getTime(); + + const localDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)); + const seconds = Math.floor((now - localDate.getTime()) / 1000); + + let interval = Math.floor(seconds / 31536000); + if (interval === 1) {return interval + ' year ago';} + if (interval > 1) {return interval + ' years ago';} + + interval = Math.floor(seconds / 2592000); + if (interval === 1) {return interval + ' month ago';} + if (interval > 1) {return interval + ' months ago';} + + interval = Math.floor(seconds / 86400); + if (interval === 1) {return interval + ' day ago';} + if (interval > 1) {return interval + ' days ago';} + + interval = Math.floor(seconds / 3600); + if (interval === 1) {return interval + ' hour ago';} + if (interval > 1) {return interval + ' hours ago';} + + interval = Math.floor(seconds / 60); + if (interval === 1) {return interval + ' minute ago';} + if (interval > 1) {return interval + ' minutes ago';} + + if(seconds < 10) return 'just now'; + + return Math.floor(seconds) + ' seconds ago'; +}; + +export function ticks_to_time(ticks: number) { + let seconds = Math.floor(ticks / 60) + let minutes = Math.floor(seconds / 60) + let hours = Math.floor(minutes / 60) + + let milliseconds = Math.floor((ticks % 60) * 1000 / 60) + seconds = seconds % 60; + minutes = minutes % 60; + + 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')}`; +}; \ No newline at end of file -- cgit v1.2.3