-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
47 lines (42 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const is = (interval, cycle) =>
cycle >= interval ? Math.round(cycle / interval) : 0;
export default function(time, now) {
if (!now) {
now = Date.now();
}
const secs = (now - time) / 1000;
const mins = is(60, secs);
const hours = is(60, mins);
const days = is(24, hours);
const weeks = is(7, days);
const months = is(30, days);
const years = is(12, months);
let amt = years;
let cycle = 'year';
if (secs <= 1) {
return 'just now';
} else if (years > 0) {
amt = years;
cycle = 'year';
} else if (months > 0) {
amt = months;
cycle = 'month';
} else if (weeks > 0) {
amt = weeks;
cycle = 'week';
} else if (days > 0) {
amt = days;
cycle = 'day';
} else if (hours > 0) {
amt = hours;
cycle = 'hour';
} else if (mins > 0) {
amt = mins;
cycle = 'minute';
} else if (secs > 0) {
amt = secs;
cycle = 'second';
}
const v = Math.round(amt);
return `${v === 1 ? (amt === hours ? 'an' : 'a') : v} ${cycle}${v > 1 ? 's' : ''} ago`;
};