forked from bahamas10/human
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (35 loc) · 1.13 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
/**
* Print a human readable timestamp to the terminal
* given a number representing seconds
*
* Author: Dave Eddy <[email protected]>
* Date: 8/18/2014
* License: MIT
*/
module.exports = human;
function human(date, includeSuffix) {
if (typeof includeSuffix === 'undefined') {
includeSuffix = true;
}
var seconds = Math.round((Date.now() - date) / 1000);
var suffix = includeSuffix ? (' ' + (seconds < 0 ? 'from now' : 'ago')) : '';
seconds = Math.abs(seconds);
var times = [
seconds / 60 / 60 / 24 / 365, // years
seconds / 60 / 60 / 24 / 30, // months
seconds / 60 / 60 / 24 / 7, // weeks
seconds / 60 / 60 / 24, // days
seconds / 60 / 60, // hours
seconds / 60, // minutes
seconds // seconds
];
var names = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second'];
for (var i = 0; i < names.length; i++) {
var time = Math.floor(times[i]);
if (time > 1)
return time + ' ' + names[i] + 's' + suffix;
else if (time === 1)
return time + ' ' + names[i] + suffix;
}
return includeSuffix ? 'just now' : 'now';
}