-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmartdate.js
320 lines (276 loc) · 9.68 KB
/
smartdate.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define('smartdate', [], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.smartdate = factory();
}
}(this, function () {
'use strict';
var smartdate = {
version: '0.9.1',
config: {
locale: 'en',
tagName: 'span',
className: 'smartdate',
addTitle: true,
updateInterval: 5000,
fullMonthNames: false,
capitalize: false,
mode: 'auto' // also available: 'past', 'future' or 'date'
}
};
var daysBetween = smartdate.daysBetween = function(date1, date2) {
// Copy date parts of the timestamps, discarding the time parts.
var one = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()),
two = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
// Do the math.
var millisecondsPerDay = 1000 * 60 * 60 * 24,
millisBetween = two.getTime() - one.getTime(),
days = millisBetween / millisecondsPerDay;
// Round down.
return Math.floor(days);
};
var pad = smartdate.pad = function(n) {
return n < 10 ? '0' + n : n;
};
smartdate.now = function() {
return new Date();
};
smartdate.locale = {
'en': {
months: [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
],
monthsShort: [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
],
date: function(date, fullMonthNames) {
var months = fullMonthNames ? this.months : this.monthsShort;
return months[date.getMonth()] + ' ' +
date.getDate() + ', ' +
date.getFullYear();
},
time: function(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = pad(minutes);
return hours + ':' + minutes + ' ' + ampm;
},
datetime: function(date, fullMonthNames) {
return this.date(date, fullMonthNames) + ' at ' + this.time(date);
},
past: function(date, fullMonthNames) {
var now = smartdate.now();
var sec = (now.getTime() - date.getTime()) / 1000;
if (sec < 60) {
return 'less than a minute ago';
} else if (sec < 60 * 60) {
return Math.floor(sec / 60) + ' min ago';
} else if (daysBetween(now, date) === 0) {
return 'today at ' + this.time(date);
} else if (daysBetween(now, date) === -1) {
return 'yesterday at ' + this.time(date);
}
return this.date(date, fullMonthNames);
},
future: function(date, fullMonthNames) {
var now = smartdate.now();
var sec = (date.getTime() - now.getTime()) / 1000;
if (sec < 60) {
return 'in less than a minute';
} else if (sec < 60 * 60) {
return 'in ' + Math.floor(sec / 60) + ' min';
} else if (daysBetween(now, date) === 0) {
return 'today at ' + this.time(date);
} else if (daysBetween(now, date) === 1) {
return 'tomorrow at ' + this.time(date);
}
return this.date(date, fullMonthNames);
}
},
'ru': {
months: [
'января', 'февраля', 'марта', 'апреля', 'мая', 'июня',
'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'
],
monthsShort: [
'янв', 'фев', 'мар', 'апр', 'мая', 'июн',
'июл', 'авг', 'сен', 'окт', 'ноя', 'дек'
],
date: function(date, fullMonthNames) {
var months = fullMonthNames ? this.months : this.monthsShort;
return date.getDate() + ' ' +
months[date.getMonth()] + ' ' +
date.getFullYear();
},
time: function(date) {
return pad(date.getHours()) + ':' + pad(date.getMinutes());
},
datetime: function(date, fullMonthNames) {
return this.date(date, fullMonthNames) + ' в ' + this.time(date);
},
past: function(date, fullMonthNames) {
var now = smartdate.now();
var sec = (now.getTime() - date.getTime()) / 1000;
if (sec < 60) {
return 'менее 1 мин назад';
} else if (sec < 60 * 60) {
return Math.floor(sec / 60) + ' мин назад';
} else if (daysBetween(now, date) === 0) {
return 'сегодня в ' + this.time(date);
} else if (daysBetween(now, date) === -1) {
return 'вчера в ' + this.time(date);
}
return this.date(date, fullMonthNames);
},
future: function(date, fullMonthNames) {
var now = smartdate.now();
var sec = (date.getTime() - now.getTime()) / 1000;
if (sec < 60) {
return 'в течение минуты';
} else if (sec < 60 * 60) {
return 'через ' + Math.floor(sec / 60) + ' мин';
} else if (daysBetween(now, date) === 0) {
return 'сегодня в ' + this.time(date);
} else if (daysBetween(now, date) === 1) {
return 'завтра в ' + this.time(date);
}
return this.date(date, fullMonthNames);
}
}
};
smartdate.getLocale = function(locale) {
locale = locale || smartdate.config.locale;
if (typeof locale === 'string') {
locale = locale.toLowerCase();
if (smartdate.locale.hasOwnProperty(locale)) {
return smartdate.locale[locale];
}
}
return smartdate.locale['en'];
};
smartdate.getOptions = function(options) {
options = options || {};
for (var o in smartdate.config) {
if (smartdate.config.hasOwnProperty(o) && !options.hasOwnProperty(o)) {
options[o] = smartdate.config[o];
}
}
return options;
};
smartdate.auto = function(locale, date, fullMonthNames) {
if (date.getTime() > smartdate.now().getTime()) {
return locale.future(date, fullMonthNames);
} else {
return locale.past(date, fullMonthNames);
}
};
/**
* String format of Date object or unix timestamp using current settings
*
* @param {Date|number} date - Date object or unix timestamp (in seconds)
* @param {object} options - optional, object with configuration options
* @return {string|null} - string representation of date made with current
* format and locale settings,
* or null if input is incorrect
*/
smartdate.format = function(date, options) {
if (!(date instanceof Date)) {
if (typeof date === 'string') {
date = Number(date);
}
if (typeof date !== 'number' || isNaN(date)) {
return null;
}
date = new Date(date * 1000);
}
options = smartdate.getOptions(options);
var dateText,
locale = smartdate.getLocale(options.locale);
if (!(options.mode && typeof locale[options.mode] === 'function')) {
// default is auto mode
dateText = smartdate.auto(locale, date, options.fullMonthNames);
} else {
dateText = locale[options.mode](date, options.fullMonthNames);
}
if (options.capitalize) {
dateText = dateText.charAt(0).toUpperCase() + dateText.slice(1);
}
return dateText;
};
smartdate.render = function() {
var config = smartdate.config,
date,
selector = config.tagName + '.' + config.className,
elements = document.querySelectorAll(selector),
el,
options,
optionValue,
timestamp;
for (var i = 0, l = elements.length; i < l; i++) {
el = elements[i];
timestamp = +el.getAttribute('data-timestamp');
// extract custom options from data-attributes
options = {};
for (var o in config) {
if (config.hasOwnProperty(o) && el.hasAttribute('data-' + o)) {
optionValue = el.getAttribute('data-' + o).toLowerCase();
if (typeof config[o] === 'boolean') {
optionValue = (optionValue === 'true');
}
options[o] = optionValue;
}
}
options = smartdate.getOptions(options);
date = new Date(timestamp * 1000);
el.innerHTML = smartdate.format(date, options);
if (options.addTitle) {
el.setAttribute('title', date.toLocaleString());
}
}
};
smartdate.setup = function(options) {
smartdate.config = smartdate.getOptions(options);
};
smartdate.init = function(options) {
smartdate.setup(options);
smartdate.render();
if (smartdate.config.updateInterval) {
window.setInterval(smartdate.render, smartdate.config.updateInterval);
}
};
smartdate.tag = function(date, options) {
var tag,
timestamp;
if (date instanceof Date) {
timestamp = Math.round(date.getTime() / 1000);
} else {
// assuming unix timestamp in seconds
timestamp = date;
date = new Date(timestamp * 1000);
}
tag = document.createElement(smartdate.config.tagName);
tag.className = smartdate.config.className;
tag.setAttribute('data-timestamp', timestamp);
options = smartdate.getOptions(options);
tag.innerHTML = smartdate.format(date, options);
if (options.addTitle) {
tag.setAttribute('title', date.toLocaleString());
}
return tag;
};
return smartdate;
}));