1function timeSince(date) {
2
3 var seconds = Math.floor((new Date() - date) / 1000);
4
5 var interval = seconds / 31536000;
6
7 if (interval > 1) {
8 return Math.floor(interval) + " years";
9 }
10 interval = seconds / 2592000;
11 if (interval > 1) {
12 return Math.floor(interval) + " months";
13 }
14 interval = seconds / 86400;
15 if (interval > 1) {
16 return Math.floor(interval) + " days";
17 }
18 interval = seconds / 3600;
19 if (interval > 1) {
20 return Math.floor(interval) + " hours";
21 }
22 interval = seconds / 60;
23 if (interval > 1) {
24 return Math.floor(interval) + " minutes";
25 }
26 return Math.floor(seconds) + " seconds";
27}
28var aDay = 24*60*60*1000;
29console.log(timeSince(new Date(Date.now()-aDay)));
30console.log(timeSince(new Date(Date.now()-aDay*2)));