( value: Date | number | string, now = new Date(), )
| 18 | !Number.isNaN(date.getTime()); |
| 19 | |
| 20 | export const publishTimeRelativeShort = ( |
| 21 | value: Date | number | string, |
| 22 | now = new Date(), |
| 23 | ): string => { |
| 24 | const date = new Date(value); |
| 25 | |
| 26 | // Calculate time delta in seconds. |
| 27 | const dt = (now.getTime() - date.getTime()) / 1000; |
| 28 | |
| 29 | if (dt <= oneMinute) { |
| 30 | return 'now'; |
| 31 | } |
| 32 | |
| 33 | if (dt <= oneHour) { |
| 34 | const numMinutes = Math.round(dt / oneMinute); |
| 35 | return `${numMinutes}m`; |
| 36 | } |
| 37 | |
| 38 | if (dt <= oneDay) { |
| 39 | const numHours = Math.round(dt / oneHour); |
| 40 | return `${numHours}h`; |
| 41 | } |
| 42 | |
| 43 | if (dt <= oneWeek) { |
| 44 | const numDays = Math.round(dt / oneDay); |
| 45 | return `${numDays}d`; |
| 46 | } |
| 47 | |
| 48 | if (dt <= oneYear) { |
| 49 | const numWeeks = Math.round(dt / oneWeek); |
| 50 | return `${numWeeks}w`; |
| 51 | } |
| 52 | |
| 53 | const numYears = Math.round(dt / oneYear); |
| 54 | return `${numYears}y`; |
| 55 | }; |
| 56 | |
| 57 | // Full, readable date for the notification timestamp tooltip — e.g. |
| 58 | // "15 April 2024 at 14:30". |
no outgoing calls
no test coverage detected