]>
Commit | Line | Data |
---|---|---|
9bf9d2a5 | 1 | import { Pipe, PipeTransform } from '@angular/core' |
b1d40cff | 2 | import { I18n } from '@ngx-translate/i18n-polyfill' |
9bf9d2a5 | 3 | |
2bbb3412 | 4 | // Thanks: https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site |
be447678 | 5 | @Pipe({ name: 'myFromNow' }) |
9bf9d2a5 C |
6 | export class FromNowPipe implements PipeTransform { |
7 | ||
b1d40cff C |
8 | constructor (private i18n: I18n) { } |
9 | ||
66be5213 | 10 | transform (arg: number | Date | string) { |
2fbe7f19 C |
11 | const argDate = new Date(arg) |
12 | const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000) | |
471ee394 | 13 | |
66be5213 RK |
14 | let interval = Math.floor(seconds / 31536000) |
15 | if (interval > 1) return this.i18n('{{interval}} years ago', { interval }) | |
16 | if (interval === 1) return this.i18n('{{interval}} year ago', { interval }) | |
9bf9d2a5 | 17 | |
66be5213 RK |
18 | interval = Math.floor(seconds / 2592000) |
19 | if (interval > 1) return this.i18n('{{interval}} months ago', { interval }) | |
20 | if (interval === 1) return this.i18n('{{interval}} month ago', { interval }) | |
9bf9d2a5 | 21 | |
66be5213 RK |
22 | interval = Math.floor(seconds / 604800) |
23 | if (interval > 1) return this.i18n('{{interval}} weeks ago', { interval }) | |
24 | if (interval === 1) return this.i18n('{{interval}} week ago', { interval }) | |
9bf9d2a5 | 25 | |
66be5213 RK |
26 | interval = Math.floor(seconds / 86400) |
27 | if (interval > 1) return this.i18n('{{interval}} days ago', { interval }) | |
28 | if (interval === 1) return this.i18n('{{interval}} day ago', { interval }) | |
29 | ||
30 | interval = Math.floor(seconds / 3600) | |
31 | if (interval > 1) return this.i18n('{{interval}} hours ago', { interval }) | |
32 | if (interval === 1) return this.i18n('{{interval}} hour ago', { interval }) | |
33 | ||
34 | interval = Math.floor(seconds / 60) | |
35 | if (interval >= 1) return this.i18n('{{interval}} min ago', { interval }) | |
36 | ||
37 | return this.i18n('just now') | |
9bf9d2a5 C |
38 | } |
39 | } |