-
Notifications
You must be signed in to change notification settings - Fork 13
/
humandate.js
180 lines (171 loc) · 4.71 KB
/
humandate.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(function () {
var humandate = {
months: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
],
monthsAbbreviated: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
],
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, monthAbbreviated) {
var monthNumber, date;
if (typeof index === 'number') {
monthNumber = index;
} else {
date = new Date(index);
monthNumber = date.getMonth() + 1;
}
return monthAbbreviated ? humandate.monthsAbbreviated[monthNumber -1] : 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.presentText) {
options.presentText = 'now';
}
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;
}
if(seconds < 1) {
return options.presentText;
}
suffix = time.past ? options.pastSuffix : options.futureSuffix;
showNext = true;
function append(amount, string) {
if (showNext) {
showNext = options.allUnits;
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');
}
if (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;
}
if (!options.monthAbbreviated) {
options.monthAbbreviated = 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, options.monthAbbreviated);
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 */
if (typeof module !== 'undefined' && module.exports) {
module.exports = humandate;
} else if (typeof define === 'function' && define.amd) {
return define([], function () {
return humandate;
});
} else {
this.humandate = humandate;
}
}());