]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/angular/from-now.pipe.ts
Add checkbox focus shadows, and admin resolution descriptions
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / angular / from-now.pipe.ts
CommitLineData
9bf9d2a5 1import { Pipe, PipeTransform } from '@angular/core'
b1d40cff 2import { 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
6export class FromNowPipe implements PipeTransform {
7
b1d40cff
C
8 constructor (private i18n: I18n) { }
9
2fbe7f19
C
10 transform (arg: number | Date | string) {
11 const argDate = new Date(arg)
12 const seconds = Math.floor((Date.now() - argDate.getTime()) / 1000)
9bf9d2a5
C
13
14 let interval = Math.floor(seconds / 31536000)
15 if (interval > 1) {
25acef90 16 return this.i18n('{{interval}} years ago', { interval })
9bf9d2a5
C
17 }
18
19 interval = Math.floor(seconds / 2592000)
25acef90
C
20 if (interval > 1) return this.i18n('{{interval}} months ago', { interval })
21 if (interval === 1) return this.i18n('{{interval}} month ago', { interval })
9bf9d2a5
C
22
23 interval = Math.floor(seconds / 604800)
25acef90
C
24 if (interval > 1) return this.i18n('{{interval}} weeks ago', { interval })
25 if (interval === 1) return this.i18n('{{interval}} week ago', { interval })
9bf9d2a5
C
26
27 interval = Math.floor(seconds / 86400)
25acef90
C
28 if (interval > 1) return this.i18n('{{interval}} days ago', { interval })
29 if (interval === 1) return this.i18n('{{interval}} day ago', { interval })
9bf9d2a5
C
30
31 interval = Math.floor(seconds / 3600)
25acef90
C
32 if (interval > 1) return this.i18n('{{interval}} hours ago', { interval })
33 if (interval === 1) return this.i18n('{{interval}} hour ago', { interval })
9bf9d2a5
C
34
35 interval = Math.floor(seconds / 60)
25acef90 36 if (interval >= 1) return this.i18n('{{interval}} min ago', { interval })
9bf9d2a5 37
ec24796e 38 return this.i18n('{{interval}} sec ago', { interval: Math.max(0, seconds) })
9bf9d2a5
C
39 }
40}