forked from panacodalabs/D8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd8.js
320 lines (272 loc) · 11 KB
/
d8.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
/*!
* D8 - The next generation Date object for JavaScript
*
* Copyright (C) 2001 by Panacoda GmbH
* MIT Licensed
*
* @author Dominik Laubach
*/
var D8 = (function() {
var date = null;
function now() {
var date = new D8();
date.date = new Date();
return date;
}
function addDays(days) {
return this.addMilliseconds(days * 24 * 60 * 60 * 1000);
}
function addHours(hours) {
return this.addMilliseconds(hours * 60 * 60 * 1000);
}
function addMinutes(minutes) {
return this.addMilliseconds(minutes * 60 * 1000);
}
function addSeconds(seconds) {
return this.addMilliseconds(seconds * 1000);
}
function addMilliseconds(milliseconds) {
var date = new D8();
date.date = new Date(this.getTimestamp() + milliseconds);
return date;
}
function tomorrow() {
return this.addDays(1);
}
function yesterday() {
return this.addDays(-1);
}
function getTimestamp() {
return this.date.getTime();
}
function timeBetween(date, returnType) {
var firstDateInMilliseconds = this.getTimestamp() ? this.getTimestamp() : null;
var secondDateInMilliseconds = date.getTimestamp() ? date.getTimestamp() : null;
if (firstDateInMilliseconds && secondDateInMilliseconds) {
switch (returnType) {
case 'days':
return (secondDateInMilliseconds - firstDateInMilliseconds) / (24 * 60 * 60 * 1000);
break;
case 'hours':
return (secondDateInMilliseconds - firstDateInMilliseconds) / (60 * 60 * 1000);
break;
case 'minutes':
return (secondDateInMilliseconds - firstDateInMilliseconds) / (60 * 1000);
break;
case 'seconds':
return (secondDateInMilliseconds - firstDateInMilliseconds) / 1000;
break;
case 'milliseconds':
default:
return (secondDateInMilliseconds - firstDateInMilliseconds);
break;
}
} else if (firstDateInMilliseconds) {
throw('Error: Invalid d8 object passed when calling timeBetween()!');
} else {
throw('Error: Trying to access timeBetween() on an invalid d8 object.');
}
}
function getCalendarWeek() {
year = parseInt(this.format('yyyy'));
month = parseInt(this.format('m'));
day = parseInt(this.format('d'));
var a = Math.floor((14 - (month)) / 12);
var y = year + 4800 - a;
var m = (month) + (12 * a) - 3;
var jd = day + Math.floor(((153 * m) + 2) / 5) + (365 * y) + Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400) - 32045;
var d4 = (jd + 31741 - (jd % 7)) % 146097 % 36524 % 1461;
var L = Math.floor(d4 / 1460);
var d1 = ((d4 - L) % 365) + L;
var calendarWeek = Math.floor(d1 / 7) + 1;
return calendarWeek;
}
function format(format, utc) {
if (isNaN(this.getTimestamp())) {
throw('Error: Trying to access format() on an invalid d8 object.');
}
var token = /d{1,4}|D{1}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g;
var timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g;
var timezoneClip = /[^-+\dA-Z]/g;
var pad = function (val, len) {
val = String(val);
len = len || 2;
while (val.length < len) val = "0" + val;
return val;
};
if (arguments.length == 1 && Object.prototype.toString.call(this.date) == "[object String]" && !/\d/.test(this.date)) {
format = this.date;
date = undefined;
}
var day_names = [
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
]
var month_names = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
];
var _ = utc ? "getUTC" : "get";
var d = this.date[_ + "Date"]();
var D = this.date[_ + "Day"]();
var m = this.date[_ + "Month"]();
var y = this.date[_ + "FullYear"]();
var H = this.date[_ + "Hours"]();
var Min = this.date[_ + "Minutes"]();
var s = this.date[_ + "Seconds"]();
var L = this.date[_ + "Milliseconds"]();
var o = utc ? 0 : this.date.getTimezoneOffset();
var flags = {
d: d,
dd: pad(d),
ddd: day_names[D],
dddd: day_names[D + 7],
D: D,
m: m + 1,
mm: pad(m + 1),
mmm: month_names[m],
mmmm: month_names[m + 12],
yy: String(y).slice(2),
yyyy: y,
h: H % 12 || 12,
hh: pad(H % 12 || 12),
H: H,
HH: pad(H),
M: Min,
MM: pad(Min),
s: s,
ss: pad(s),
l: pad(L, 3),
L: pad(L > 99 ? Math.round(L / 10) : L),
t: H < 12 ? "a" : "p",
tt: H < 12 ? "am" : "pm",
T: H < 12 ? "A" : "P",
TT: H < 12 ? "AM" : "PM",
Z: utc ? "UTC" : (String(this).match(timezone) || [""]).pop().replace(timezoneClip, ""),
o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
};
return format.replace(token, function ($0) {
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
}
function getDatesOfCalendarWeek(startWeekOnMonday) {
year = this.format('yyyy');
var newYear = this.create('01/01/' + year);
var newYearWeekDay = newYear.format('D');
var firstWeek = null;
if (startWeekOnMonday) {
firstWeek = newYearWeekDay == 1 ? newYear : newYear.addDays(8 - (newYearWeekDay == 0 ? 7 : newYearWeekDay));
} else {
firstWeek = newYearWeekDay == 0 ? newYear : newYear.addDays(7 - newYearWeekDay);
}
var calendarWeek = this.getCalendarWeek();
var requiredWeek = firstWeek.addDays((calendarWeek - 1) * 7);
var dates = [];
for (var i = 0; i < 7; i++) {
var date = requiredWeek.addDays(i);
date = this.create(date.format('mm') + '/' + date.format('dd') + '/' + date.format('yyyy'));
dates.push(date);
}
return dates;
}
function create(dateString) {
var milliseconds = typeof(dateString) === 'number' ? dateString : null;
if (!milliseconds) {
var regexResult = /(\d{1,2})\.(\d{1,2})\.(\d{2,4})/.exec(dateString);
if (regexResult && regexResult[1] && regexResult[2] && regexResult[3]) {
var date = dateString.split(' ');
dateString = regexResult[2] + '/' + regexResult[1] + '/' + regexResult[3] + (date[1] ? ' ' + date[1] : '');
}
milliseconds = Date.parse(dateString);
}
if (dateString && !milliseconds) {
throw('Error: Invalid dateString \'' + dateString + '\' passed to create().');
} else if (!dateString) {
return this.now();
}
var date = new D8();
date.date = new Date(milliseconds);
return date;
}
return {
date: new Date(),
now: now,
addDays: addDays,
addHours: addHours,
addMinutes: addMinutes,
addSeconds: addSeconds,
addMilliseconds: addMilliseconds,
timeBetween: timeBetween,
yesterday: yesterday,
tomorrow: tomorrow,
getTimestamp: getTimestamp,
getCalendarWeek: getCalendarWeek,
format: format,
getDatesOfCalendarWeek: getDatesOfCalendarWeek,
create: create
};
});
D8.getCalendarWeek = (function(year, month, day) {
var a = Math.floor((14 - (month)) / 12);
var y = year + 4800 - a;
var m = (month) + (12 * a) - 3;
var jd = day + Math.floor(((153 * m) + 2) / 5) + (365 * y) + Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400) - 32045;
var d4 = (jd + 31741 - (jd % 7)) % 146097 % 36524 % 1461;
var L = Math.floor(d4 / 1460);
var d1 = ((d4 - L) % 365) + L;
var calendarWeek = Math.floor(d1 / 7) + 1;
return calendarWeek;
});
D8.getDatesOfCalendarWeek = (function(calendarWeek, startWeekOnMonday, year) {
year = year && !isNaN(year) ? year : (this.date ? this.format('yyyy') : this.now().format('yyyy'));
var newYear = this.create('01/01/' + year);
var newYearWeekDay = newYear.format('D');
var firstWeek = null;
if (startWeekOnMonday) {
firstWeek = newYearWeekDay == 1 ? newYear : newYear.addDays(8 - (newYearWeekDay == 0 ? 7 : newYearWeekDay));
} else {
firstWeek = newYearWeekDay == 0 ? newYear : newYear.addDays(7 - newYearWeekDay);
}
calendarWeek = calendarWeek ? calendarWeek : this.getCalendarWeek();
var requiredWeek = firstWeek.addDays((calendarWeek - 1) * 7);
var dates = [];
for (var i = 0; i < 7; i++) {
var date = requiredWeek.addDays(i);
date = this.create(date.format('mm') + '/' + date.format('dd') + '/' + date.format('yyyy'));
dates.push(date);
}
return dates;
});
D8.getDateByWeekdayAndCalendarWeek = (function(calendarWeek, dayOfWeek, year) {
if (calendarWeek && !isNaN(calendarWeek) && ((dayOfWeek && !isNaN(dayOfWeek)) || dayOfWeek === 0)) {
var dates = this.getDatesOfCalendarWeek(calendarWeek, false, year);
if (dates && dates.length > 0 && dates[dayOfWeek]) {
return dates[dayOfWeek];
} else {
throw('Error: Day ' + dayOfWeek + ' of calendar week ' + calendarWeek + ' could not be found!');
}
} else {
throw('Error: Please pass a valid calendarWeek and a valid day of the week!');
}
});
D8.create = (function(dateString) {
var milliseconds = typeof(dateString) === 'number' ? dateString : null;
if (!milliseconds) {
var regexResult = /(\d{1,2})\.(\d{1,2})\.(\d{2,4})/.exec(dateString);
if (regexResult && regexResult[1] && regexResult[2] && regexResult[3]) {
var date = dateString.split(' ');
dateString = regexResult[2] + '/' + regexResult[1] + '/' + regexResult[3] + (date[1] ? ' ' + date[1] : '');
}
milliseconds = Date.parse(dateString);
}
if (dateString && !milliseconds) {
throw('Error: Invalid dateString \'' + dateString + '\' passed to create().');
} else if (!dateString) {
return this.now();
}
var date = new D8();
date.date = new Date(milliseconds);
return date;
});
var Ø = D8;