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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 | 1
1
5
5
5
20
20
14
6
6
20
10
10
10
6
4
4
4
4
10
7
10
9
10
9
10
9
10
10
10
10
1
9
9
1
33
9
9
9
6
9
6
9
6
9
6
9
9
9
11
11
1
10
1
11
10
11
10
11
11
11
7
4
1
3
1
2
1
1
11
11
11
11
11
11
11
11
11
11
1
1
1
1
1
1
| (function () {
var humandate = {
months: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
],
toUTC: function toUTC(input) {
var date = input ? new Date(input) : new Date();
date = new Date(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds()
);
return date;
},
monthName: function monthName(index) {
var monthNumber, date;
if (typeof index === 'number') {
monthNumber = index;
} else {
date = new Date(index);
monthNumber = date.getMonth() + 1;
}
return humandate.months[monthNumber - 1];
},
relativeTime: function relativeTime(input, options) {
var seconds, time, suffix, then, date, now, isPast, showNext;
var output = [];
if (typeof input === 'number') {
seconds = input;
} else {
date = new Date(input);
then = date.getTime();
now = new Date().getTime();
seconds = (now - then) / 1000 * -1;
}
if (!options) {
options = {};
}
if (!options.futureSuffix) {
options.futureSuffix = 'from now';
}
if (!options.pastSuffix) {
options.pastSuffix = 'ago';
}
if (!options.returnObject) {
options.returnObject = false;
}
isPast = seconds < 0 ? true : false;
seconds = Math.abs(seconds);
time = {
seconds: Math.floor(seconds % 31536000 % 86400 % 3600 % 60),
minutes: Math.floor(seconds % 31536000 % 86400 % 3600 / 60),
hours: Math.floor(seconds % 31536000 % 86400 / 3600),
days: Math.floor(seconds % 31536000 / 86400),
years: Math.floor(seconds / 31536000),
past: isPast
};
if (options.returnObject)
return time;
suffix = time.past ? options.pastSuffix : options.futureSuffix;
showNext = true;
function append(amount, string) {
if (showNext) {
showNext = false;
output.push(amount + ' ' + string + (amount > 1 ? 's' : ''));
}
}
if (time.years) {
append(time.years, 'year');
}
if (time.days) {
append(time.days, 'day');
}
if (time.hours) {
append(time.hours, 'hour');
}
if (time.minutes) {
append(time.minutes, 'minute');
}
Eif (time.seconds) {
append(time.seconds, 'second');
}
return output.join(', ') + ' ' + suffix;
},
prettyPrint: function prettyPrint(input, options) {
var date, hdate, day, humanDate, year, month, tstr, hours, minutes, ampm;
if (!input) {
input = new Date();
} else if (typeof input === 'number') {
input = new Date().setSeconds(input);
}
if (!options) {
options = {};
}
if (!options.showTime) {
options.showTime = false;
}
date = new Date(input);
day = date.getDate();
if (day > 3 && day < 21) {
humanDate = day + 'th';
} else if (day % 10 === 1) {
humanDate = day + 'st';
} else if (day % 10 === 2) {
humanDate = day + 'nd';
} else if (day % 10 === 3) {
humanDate = day + 'rd';
} else {
humanDate = day + 'th';
}
year = date.getFullYear();
month = this.monthName(date.getMonth() + 1);
hdate = month + ' ' + humanDate + ', ' + year;
hours = date.getHours();
minutes = date.getMinutes();
ampm = hours >= 12 ? 'pm' : 'am';
hours = (hours % 12) ? hours % 12 : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
tstr = hours + ':' + minutes + ' ' + ampm;
return options.showTime ? hdate + " at " + tstr : hdate;
}
};
/* istanbul ignore next: code loaders */
Eif (typeof module !== 'undefined' && module.exports) {
module.exports = humandate;
} else if (typeof define === 'function' && define.amd) {
return define([], function () {
return humandate;
});
} else {
this.humandate = humandate;
}
}());
|