diff options
| author | Factiven <[email protected]> | 2023-09-25 00:44:40 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-25 00:44:40 +0700 |
| commit | 1a85c2571690ba592ac5183d5eadaf9846fe532b (patch) | |
| tree | 3f3552c00cd49c0eeab5275275cf5cf5666e5027 /utils/getTimes.js | |
| parent | Delete .github/workflows/deploy.yml (diff) | |
| download | moopa-1a85c2571690ba592ac5183d5eadaf9846fe532b.tar.xz moopa-1a85c2571690ba592ac5183d5eadaf9846fe532b.zip | |
Update v4.1.0 (#79)v4.1.0
* Update v4.1.0
* Update pages/_app.js
Diffstat (limited to 'utils/getTimes.js')
| -rw-r--r-- | utils/getTimes.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/utils/getTimes.js b/utils/getTimes.js index 8bbc2ee..d06f797 100644 --- a/utils/getTimes.js +++ b/utils/getTimes.js @@ -106,3 +106,29 @@ export const timeStamptoHour = (timestamp) => { return `${status} at ${formattedTime}`; }; + +export function unixTimestampToRelativeTime(unixTimestamp) { + const now = Math.floor(Date.now() / 1000); // Current Unix timestamp in seconds + const secondsAgo = now - unixTimestamp; + + const intervals = [ + { label: "year", seconds: 31536000 }, + { label: "month", seconds: 2592000 }, + { label: "week", seconds: 604800 }, + { label: "day", seconds: 86400 }, + { label: "hour", seconds: 3600 }, + { label: "minute", seconds: 60 }, + { label: "second", seconds: 1 }, + ]; + + for (const interval of intervals) { + const count = Math.floor(secondsAgo / interval.seconds); + if (count >= 1) { + return count === 1 + ? ` ${count} ${interval.label} ago` + : ` ${count} ${interval.label}s ago`; + } + } + + return "just now"; +} |