diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-12-11 11:06:32 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-12-11 11:06:32 +0100 |
commit | fada8d75550dc7365f7e18ee1569b9406251d660 (patch) | |
tree | db9dc01c18693824f83fce5020f4c1f3ae7c0865 /client/src/app/shared/misc/from-now.pipe.ts | |
parent | 492fd28167f770d79a430fc57451b5a9e075d8e7 (diff) | |
parent | c2830fa8f84f61462098bf36add824f89436dfa9 (diff) | |
download | PeerTube-fada8d75550dc7365f7e18ee1569b9406251d660.tar.gz PeerTube-fada8d75550dc7365f7e18ee1569b9406251d660.tar.zst PeerTube-fada8d75550dc7365f7e18ee1569b9406251d660.zip |
Merge branch 'feature/design' into develop
Diffstat (limited to 'client/src/app/shared/misc/from-now.pipe.ts')
-rw-r--r-- | client/src/app/shared/misc/from-now.pipe.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/client/src/app/shared/misc/from-now.pipe.ts b/client/src/app/shared/misc/from-now.pipe.ts new file mode 100644 index 000000000..fac02af0b --- /dev/null +++ b/client/src/app/shared/misc/from-now.pipe.ts | |||
@@ -0,0 +1,36 @@ | |||
1 | import { Pipe, PipeTransform } from '@angular/core' | ||
2 | |||
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' }) | ||
5 | export class FromNowPipe implements PipeTransform { | ||
6 | |||
7 | transform (value: number) { | ||
8 | const seconds = Math.floor((Date.now() - value) / 1000) | ||
9 | |||
10 | let interval = Math.floor(seconds / 31536000) | ||
11 | if (interval > 1) { | ||
12 | return interval + ' years ago' | ||
13 | } | ||
14 | |||
15 | interval = Math.floor(seconds / 2592000) | ||
16 | if (interval > 1) return interval + ' months ago' | ||
17 | if (interval === 1) return interval + ' month ago' | ||
18 | |||
19 | interval = Math.floor(seconds / 604800) | ||
20 | if (interval > 1) return interval + ' weeks ago' | ||
21 | if (interval === 1) return interval + ' week ago' | ||
22 | |||
23 | interval = Math.floor(seconds / 86400) | ||
24 | if (interval > 1) return interval + ' days ago' | ||
25 | if (interval === 1) return interval + ' day ago' | ||
26 | |||
27 | interval = Math.floor(seconds / 3600) | ||
28 | if (interval > 1) return interval + ' hours ago' | ||
29 | if (interval === 1) return interval + ' hour ago' | ||
30 | |||
31 | interval = Math.floor(seconds / 60) | ||
32 | if (interval >= 1) return interval + ' min ago' | ||
33 | |||
34 | return Math.floor(seconds) + ' sec ago' | ||
35 | } | ||
36 | } | ||