diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/src/app/shared/shared-main/angular/from-now.pipe.ts | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/client/src/app/shared/shared-main/angular/from-now.pipe.ts b/client/src/app/shared/shared-main/angular/from-now.pipe.ts index 5e7832807..d62c1f88e 100644 --- a/client/src/app/shared/shared-main/angular/from-now.pipe.ts +++ b/client/src/app/shared/shared-main/angular/from-now.pipe.ts | |||
@@ -3,32 +3,37 @@ import { Pipe, PipeTransform } from '@angular/core' | |||
3 | // Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site | 3 | // Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site |
4 | @Pipe({ name: 'myFromNow' }) | 4 | @Pipe({ name: 'myFromNow' }) |
5 | export class FromNowPipe implements PipeTransform { | 5 | export class FromNowPipe implements PipeTransform { |
6 | |||
7 | transform (arg: number | Date | string) { | 6 | transform (arg: number | Date | string) { |
8 | const argDate = new Date(arg) | 7 | const argDate = new Date(arg) |
9 | const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000) | 8 | const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000) |
10 | 9 | ||
11 | let interval = Math.round(seconds / 31536000) | 10 | let interval = Math.floor(seconds / 31536000) |
12 | if (interval > 1) return $localize`${interval} years ago` | 11 | if (interval > 1) return $localize`${interval} years ago` |
13 | if (interval === 1) return $localize`${interval} year ago` | 12 | if (interval === 1) return $localize`1 year ago` |
14 | 13 | ||
15 | interval = Math.round(seconds / 2592000) | 14 | interval = Math.floor(seconds / 2419200) |
15 | // 12 months = 360 days, but a year ~ 365 days | ||
16 | // Display "1 year ago" rather than "12 months ago" | ||
17 | if (interval >= 12) return $localize`1 year ago` | ||
16 | if (interval > 1) return $localize`${interval} months ago` | 18 | if (interval > 1) return $localize`${interval} months ago` |
17 | if (interval === 1) return $localize`${interval} month ago` | 19 | if (interval === 1) return $localize`1 month ago` |
18 | 20 | ||
19 | interval = Math.round(seconds / 604800) | 21 | interval = Math.floor(seconds / 604800) |
22 | // 4 weeks ~ 28 days, but our month is 30 days | ||
23 | // Display "1 month ago" rather than "4 weeks ago" | ||
24 | if (interval >= 4) return $localize`1 month ago` | ||
20 | if (interval > 1) return $localize`${interval} weeks ago` | 25 | if (interval > 1) return $localize`${interval} weeks ago` |
21 | if (interval === 1) return $localize`${interval} week ago` | 26 | if (interval === 1) return $localize`1 week ago` |
22 | 27 | ||
23 | interval = Math.round(seconds / 86400) | 28 | interval = Math.floor(seconds / 86400) |
24 | if (interval > 1) return $localize`${interval} days ago` | 29 | if (interval > 1) return $localize`${interval} days ago` |
25 | if (interval === 1) return $localize`${interval} day ago` | 30 | if (interval === 1) return $localize`1 day ago` |
26 | 31 | ||
27 | interval = Math.round(seconds / 3600) | 32 | interval = Math.floor(seconds / 3600) |
28 | if (interval > 1) return $localize`${interval} hours ago` | 33 | if (interval > 1) return $localize`${interval} hours ago` |
29 | if (interval === 1) return $localize`${interval} hour ago` | 34 | if (interval === 1) return $localize`1 hour ago` |
30 | 35 | ||
31 | interval = Math.round(seconds / 60) | 36 | interval = Math.floor(seconds / 60) |
32 | if (interval >= 1) return $localize`${interval} min ago` | 37 | if (interval >= 1) return $localize`${interval} min ago` |
33 | 38 | ||
34 | return $localize`just now` | 39 | return $localize`just now` |