| 37 | } |
| 38 | |
| 39 | export function getShorthandTimeIntervalString( |
| 40 | oldTime: Date, |
| 41 | negate: boolean |
| 42 | ): string { |
| 43 | const now = new Date(); |
| 44 | let timeDiffMs: number = now.getTime() - oldTime.getTime(); |
| 45 | if (negate) { |
| 46 | timeDiffMs *= -1; |
| 47 | } |
| 48 | // --- If negative, just display zero --- |
| 49 | if (timeDiffMs <= 0) { |
| 50 | return '0m'; |
| 51 | } |
| 52 | // --- Within the last minute --- |
| 53 | if (timeDiffMs < MS_PER_MINUTE) { |
| 54 | return `${Math.floor(timeDiffMs / MS_PER_SECOND)}s`; |
| 55 | } |
| 56 | // --- See if the last message was sent within the last hour --- |
| 57 | if (timeDiffMs < MS_PER_HOUR) { |
| 58 | return `${Math.floor(timeDiffMs / MS_PER_MINUTE)}m`; |
| 59 | } |
| 60 | // --- Within the last day (display hours) --- |
| 61 | if (timeDiffMs < MS_PER_DAY) { |
| 62 | return `${Math.floor(timeDiffMs / MS_PER_HOUR)}h`; |
| 63 | } |
| 64 | |
| 65 | // --- Within the last week (display days) --- |
| 66 | if (timeDiffMs < MS_PER_WEEK) { |
| 67 | return `${Math.floor(timeDiffMs / MS_PER_DAY)}d`; |
| 68 | } |
| 69 | |
| 70 | // --- Default: Display weeks --- |
| 71 | // return `${Math.floor(timeDiffMs / MS_PER_WEEK)}w`; |
| 72 | |
| 73 | // --- Within the last month (display weeks) --- |
| 74 | if (timeDiffMs < MS_PER_MONTH) { |
| 75 | return `${Math.floor(timeDiffMs / MS_PER_WEEK)}w`; |
| 76 | } |
| 77 | |
| 78 | // --- Within the last year (display months) --- |
| 79 | if (timeDiffMs < MS_PER_YEAR) { |
| 80 | return `${Math.floor(timeDiffMs / MS_PER_MONTH)}m`; |
| 81 | } |
| 82 | |
| 83 | // --- More than a year (display number of years) --- |
| 84 | return `${Math.floor(timeDiffMs / MS_PER_YEAR)}y`; |
| 85 | } |