1function timeDifference(current, previous) {
2
3 var msPerMinute = 60 * 1000;
4 var msPerHour = msPerMinute * 60;
5 var msPerDay = msPerHour * 24;
6 var msPerMonth = msPerDay * 30;
7 var msPerYear = msPerDay * 365;
8
9 var elapsed = current - previous;
10
11 if (elapsed < msPerMinute) {
12 return Math.round(elapsed/1000) + ' seconds ago';
13 }
14
15 else if (elapsed < msPerHour) {
16 return Math.round(elapsed/msPerMinute) + ' minutes ago';
17 }
18
19 else if (elapsed < msPerDay ) {
20 return Math.round(elapsed/msPerHour ) + ' hours ago';
21 }
22
23 else if (elapsed < msPerMonth) {
24 return 'approximately ' + Math.round(elapsed/msPerDay) + ' days ago';
25 }
26
27 else if (elapsed < msPerYear) {
28 return 'approximately ' + Math.round(elapsed/msPerMonth) + ' months ago';
29 }
30
31 else {
32 return 'approximately ' + Math.round(elapsed/msPerYear ) + ' years ago';
33 }
34}
35