|
1 |
| -// shamelessly copied from: |
2 |
| -// http://stackoverflow.com/questions/6108819/javascript-timestamp-to-relative-time-eg-2-seconds-ago-one-week-ago-etc-best |
3 | 1 | function timeDifference(current, previous) {
|
4 | 2 |
|
5 |
| - var msPerMinute = 60 * 1000; |
6 |
| - var msPerHour = msPerMinute * 60; |
7 |
| - var msPerDay = msPerHour * 24; |
8 |
| - var msPerMonth = msPerDay * 30; |
9 |
| - var msPerYear = msPerDay * 365; |
| 3 | + const milliSecondsPerMinute = 60 * 1000 |
| 4 | + const milliSecondsPerHour = milliSecondsPerMinute * 60 |
| 5 | + const milliSecondsPerDay = milliSecondsPerHour * 24 |
| 6 | + const milliSecondsPerMonth = milliSecondsPerDay * 30 |
| 7 | + const milliSecondsPerYear = milliSecondsPerDay * 365 |
10 | 8 |
|
11 |
| - var elapsed = current - previous; |
| 9 | + const elapsed = current - previous |
12 | 10 |
|
13 |
| - if (elapsed < msPerMinute / 3) { |
| 11 | + if (elapsed < milliSecondsPerMinute / 3) { |
14 | 12 | return 'just now'
|
15 | 13 | }
|
16 | 14 |
|
17 |
| - if (elapsed < msPerMinute) { |
| 15 | + if (elapsed < milliSecondsPerMinute) { |
18 | 16 | return 'less than 1 min ago'
|
19 | 17 | }
|
20 | 18 |
|
21 |
| - else if (elapsed < msPerHour) { |
22 |
| - return Math.round(elapsed/msPerMinute) + ' min ago'; |
| 19 | + else if (elapsed < milliSecondsPerHour) { |
| 20 | + return Math.round(elapsed/milliSecondsPerMinute) + ' min ago' |
23 | 21 | }
|
24 | 22 |
|
25 |
| - else if (elapsed < msPerDay ) { |
26 |
| - return Math.round(elapsed/msPerHour ) + ' h ago'; |
| 23 | + else if (elapsed < milliSecondsPerDay ) { |
| 24 | + return Math.round(elapsed/milliSecondsPerHour ) + ' h ago' |
27 | 25 | }
|
28 | 26 |
|
29 |
| - else if (elapsed < msPerMonth) { |
30 |
| - return Math.round(elapsed/msPerDay) + ' days ago'; |
| 27 | + else if (elapsed < milliSecondsPerMonth) { |
| 28 | + return Math.round(elapsed/milliSecondsPerDay) + ' days ago' |
31 | 29 | }
|
32 | 30 |
|
33 |
| - else if (elapsed < msPerYear) { |
34 |
| - return Math.round(elapsed/msPerMonth) + ' mo ago'; |
| 31 | + else if (elapsed < milliSecondsPerYear) { |
| 32 | + return Math.round(elapsed/milliSecondsPerMonth) + ' mo ago' |
35 | 33 | }
|
36 | 34 |
|
37 | 35 | else {
|
38 |
| - return Math.round(elapsed/msPerYear ) + ' years ago'; |
| 36 | + return Math.round(elapsed/milliSecondsPerYear ) + ' years ago' |
39 | 37 | }
|
40 | 38 | }
|
41 | 39 |
|
|
0 commit comments