-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery-sensible-datetime.js
226 lines (206 loc) · 6.18 KB
/
jquery-sensible-datetime.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
/*
* sensible-datetime: a jQuery plugin, version: 0.0.1
* Last modified: 09/26/2011
*
* Sensible DateTime is a jQuery plugin that makes it easy to format ISO datetime into something sensible as you see fit.
*
* http://crossbreeze.github.com/jquery-sensible-datetime/
*
* Licensed under the MIT:
* http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2011, Jaewoong Kim ([email protected])
*/
(function($) {
$.fn.sensible= function(options) {
if (options) {
$.extend(settings , options);
}
var self = this;
self.each(refresh);
setInterval(function() {
self.each(refresh);
}, settings.refreshRate);
return self;
};
var settings = {
shortDayNames: [
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
],
longDayNames: [
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
],
shortMonthNames: [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
],
longMonthNames: [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
],
refreshRate: 60000,
pastMask: '%mmmm %d, %yyyy at %h:%MM%tt',
futureMask: '%mmmm %d, %yyyy at %h:%MM%tt',
masks: [
{
// within a minute
distance: 60,
mask: 'just now'
},
{
// within 2 minutes
distance: 120,
mask: 'about a minute ago'
},
{
// within 1 hour
distance: 3600,
mask: '%xm minutes ago'
},
{
// within 2 hours
distance: 7200,
mask: 'about an hour ago'
},
{
// within 24 hours
distance: 86400,
mask: '%xh hours ago'
},
{
// within 48 hours
distance: 172800,
mask: 'Yesterday at %h:%MM%tt'
},
{
// within this year
distance: 31556926,
mask: '%mmmm %d at %h:%MM%tt'
}
]
};
// private functions
function refresh() {
var datetime = prepareData(this);
if (datetime) {
$(this).text(datetimeInWords(datetime));
return this;
}
}
function prepareData(element) {
element = $(element);
if (!element.data('datetime')) {
element.data('datetime', datetime(element));
var text = $.trim(element.text());
if (text.length > 0) {
element.attr('title', text);
}
}
return element.data('datetime');
}
function datetime(elem) {
var isTime = $(elem).get(0).tagName.toLowerCase() === 'time';
var iso8601 = isTime ? $(elem).attr('datetime') : $(elem).attr('title');
return parse(iso8601);
}
function parse(iso8601) {
var s = $.trim(iso8601);
s = s.replace(/\.\d\d\d+/,'');
s = s.replace(/-/,'/').replace(/-/,'/');
s = s.replace(/T/,' ').replace(/Z/,' UTC');
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/,' $1$2');
var datetime = new Date(s);
return isNaN(datetime) ? null : datetime;
}
function datetimeInWords(datetime) {
var distanceSeconds = (new Date().getTime() - datetime.getTime()) / 1000;
if (distanceSeconds < 0) {
return stringFromMask(datetime, settings.futureMask);
}
for (var i = 0, length = settings.masks.length; i < length; ++i) {
if (distanceSeconds < settings.masks[i].distance) {
return stringFromMask(datetime, settings.masks[i].mask);
}
}
return stringFromMask(datetime, settings.pastMask);
}
function stringFromMask(datetime, mask) {
var re = /\%(d{1,4}|m{1,4}|yy(?:yy)?|[HhMsTt]{1,2}|x[smhdy]|[S])/g;
var distanceSeconds = (new Date().getTime() - datetime.getTime()) / 1000;
var distanceMinutes = distanceSeconds / 60;
var distanceHours = distanceMinutes / 60;
var distanceDays = distanceHours / 24;
var distanceYears = distanceDays / 365;
return mask.replace(re, function(match) {
switch (match.substr(1, match.length)) {
case 'd':
return datetime.getDate();
case 'dd':
return pad(datetime.getDate());
case 'ddd':
return settings.shortDayNames[datetime.getDay()];
case 'dddd':
return settings.longDayNames[datetime.getDay()];
case 'm':
return datetime.getMonth() + 1;
case 'mm':
return pad(datetime.getMonth() + 1);
case 'mmm':
return settings.shortMonthNames[datetime.getMonth()];
case 'mmmm':
return settings.longMonthNames[datetime.getMonth()];
case 'yy':
return String(datetime.getFullYear()).slice(2);
case 'yyyy':
return datetime.getFullYear();
case 'h':
return datetime.getHours() % 12 || 12;
case 'hh':
return pad(datetime.getHours() % 12 || 12);
case 'H':
return datetime.getHours();
case 'HH':
return pad(datetime.getHours());
case 'M':
return datetime.getMinutes();
case 'MM':
return pad(datetime.getMinutes());
case 's':
return datetime.getSeconds();
case 'ss':
return pad(datetime.getSeconds());
case 't':
return datetime.getHours() < 12 ? 'a' : 'p';
case 'tt':
return datetime.getHours() < 12 ? 'am' : 'pm';
case 'T':
return datetime.getHours() < 12 ? 'A' : 'P';
case 'TT':
return datetime.getHours() < 12 ? 'AM' : 'PM';
case 'S':
return ['th', 'st', 'nd', 'rd'][datetime.getDate() % 10 > 3 ? 0 : (datetime.getDate() % 100 - datetime.getDate() % 10 != 10) * datetime.getDate() % 10];
case 'xs':
return Math.round(distanceSeconds);
case 'xm':
return Math.round(distanceMinutes);
case 'xh':
return Math.round(distanceHours);
case 'xd':
return Math.floor(distanceDays);
case 'xy':
return Math.floor(distanceYears);
default:
return match;
}
});
}
function pad(val, len) {
val = String(val);
len = len || 2;
while (val.length < len) {
val = '0' + val;
}
return val;
}
// fix for IE6
document.createElement('abbr');
document.createElement('time');
})(jQuery);