(timestamp: number)
| 27 | } |
| 28 | |
| 29 | export const formatTimeAgo = (timestamp: number) => { |
| 30 | const now = Date.now() |
| 31 | const diff = now - timestamp |
| 32 | const seconds = Math.floor(diff / 1000) |
| 33 | const minutes = Math.floor(seconds / 60) |
| 34 | const hours = Math.floor(minutes / 60) |
| 35 | const days = Math.floor(hours / 24) |
| 36 | const weeks = Math.floor(days / 7) |
| 37 | const months = Math.floor(days / 30) |
| 38 | const years = Math.floor(days / 365) |
| 39 | |
| 40 | if (years > 0) { |
| 41 | return years === 1 |
| 42 | ? i18next.t("common:time_ago.year_ago") |
| 43 | : i18next.t("common:time_ago.years_ago", { count: years }) |
| 44 | } |
| 45 | if (months > 0) { |
| 46 | return months === 1 |
| 47 | ? i18next.t("common:time_ago.month_ago") |
| 48 | : i18next.t("common:time_ago.months_ago", { count: months }) |
| 49 | } |
| 50 | if (weeks > 0) { |
| 51 | return weeks === 1 |
| 52 | ? i18next.t("common:time_ago.week_ago") |
| 53 | : i18next.t("common:time_ago.weeks_ago", { count: weeks }) |
| 54 | } |
| 55 | if (days > 0) { |
| 56 | return days === 1 |
| 57 | ? i18next.t("common:time_ago.day_ago") |
| 58 | : i18next.t("common:time_ago.days_ago", { count: days }) |
| 59 | } |
| 60 | if (hours > 0) { |
| 61 | return hours === 1 |
| 62 | ? i18next.t("common:time_ago.hour_ago") |
| 63 | : i18next.t("common:time_ago.hours_ago", { count: hours }) |
| 64 | } |
| 65 | if (minutes > 0) { |
| 66 | return minutes === 1 |
| 67 | ? i18next.t("common:time_ago.minute_ago") |
| 68 | : i18next.t("common:time_ago.minutes_ago", { count: minutes }) |
| 69 | } |
| 70 | if (seconds > 30) { |
| 71 | return i18next.t("common:time_ago.seconds_ago", { count: seconds }) |
| 72 | } |
| 73 | |
| 74 | return i18next.t("common:time_ago.just_now") |
| 75 | } |
no outgoing calls
no test coverage detected