-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsamay.js
360 lines (295 loc) · 11.5 KB
/
samay.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
'use strict';
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/* global window */
function calcDate(structuredDate) {
try {
var splitDate = structuredDate.split('T'),
datePart = splitDate[0].split('-'),
timePart = splitDate[1].replace('+05:30', '').split(':');
var date = new Date();
date.setFullYear(datePart[0]);
date.setMonth(+datePart[1] - 1);
date.setDate(datePart[2]);
date.setHours(timePart[0]);
date.setMinutes(timePart[1]);
date.setSeconds(timePart[2]);
return date;
} catch (e) {
return new Date(structuredDate);
}
}
function parseDate(strDate) {
var REGEXP_TO_EXTRACT_DATE = /(\d{4})(\d{2})(\d{2})/g,
splitDate = REGEXP_TO_EXTRACT_DATE.exec(strDate);
if (splitDate) {
var date = [splitDate[1], splitDate[2], splitDate[3]].join('-'),
structuredDate = date + 'T00:00:00+05:30'; // iOS needs format 2020-12-30T23:30:10+05:30 , default parses into GMT time, so need +05:30 for IST conversion
return structuredDate;
} else {
return strDate;
}
}
// Function for format 20170317
function parseDateTime(strDate) {
var REGEXP_TO_EXTRACT_DATE = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/g,
splitDate = REGEXP_TO_EXTRACT_DATE.exec(strDate);
if (splitDate) {
var date = [splitDate[1], splitDate[2], splitDate[3]].join('-'),
time = [splitDate[4], splitDate[5]].join(':'),
structuredDate = date + 'T' + time + ':00+05:30'; // iOS needs format 2020-12-30T23:30:10+05:30 , default parses into GMT time, so need +05:30 for IST conversion
return structuredDate;
} else {
return strDate;
}
}
// Function for format 17-03-2017 22:10:30
function parseDateTime2(strDate) {
var REGEXP_TO_EXTRACT_DATE = /(\d{2})-(\d{2})-(\d{4})\s(\d{2}):(\d{2}):(\d{2})/g,
splitDate = REGEXP_TO_EXTRACT_DATE.exec(strDate);
if (splitDate) {
var date = [splitDate[3], splitDate[2], splitDate[1]].join('-'),
time = [splitDate[4], splitDate[5], splitDate[6]].join(':'),
structuredDate = date + 'T' + time + '+05:30'; // iOS needs format 2020-12-30T23:30:10+05:30 , default parses into GMT time, so need +05:30 for IST conversion
return structuredDate;
} else {
return strDate;
}
}
function parseDateTime3(strDate) {
var REGEXP_TO_EXTRACT_DATE = /(\w{3}),\s(\d{2})\s(\w{3}),\s(\d{4})/g,
splitDate = REGEXP_TO_EXTRACT_DATE.exec(strDate);
if (splitDate) {
var month = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'].indexOf(splitDate[3].toLowerCase()),
date = [splitDate[4], _prepandZero(month + 1), splitDate[2]].join('-'),
structuredDate = date + 'T00:00:00+05:30'; // iOS needs format 2020-12-30T23:30:10+05:30 , default parses into GMT time, so need +05:30 for IST conversion
return structuredDate;
} else {
return strDate;
}
}
function parseDateTime4(strDate) {
var REGEXP_TO_EXTRACT_DATE = /(\d{2})\/(\d{2})\/(\d{4})/g,
splitDate = REGEXP_TO_EXTRACT_DATE.exec(strDate);
if (splitDate) {
var date = [splitDate[3], splitDate[2], splitDate[1]].join('-'),
structuredDate = date + 'T00:00:00+05:30'; // iOS needs format 2020-12-30T23:30:10+05:30 , default parses into GMT time, so need +05:30 for IST conversion
return structuredDate;
} else {
return strDate;
}
}
// Parse format MMM DD, YYYY
function parseDateTime5(strDate) {
var REGEXP_TO_EXTRACT_DATE = /(\w{3})\s(\w{2}),\s(\d{4})/g,
splitDate = REGEXP_TO_EXTRACT_DATE.exec(strDate);
if (splitDate) {
var month = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'].indexOf(splitDate[1].toLowerCase()),
date = [splitDate[3], _prepandZero(month + 1), splitDate[2]].join('-'),
structuredDate = date + 'T00:00:00+05:30'; // iOS needs format 2020-12-30T23:30:10+05:30 , default parses into GMT time, so need +05:30 for IST conversion
return structuredDate;
} else {
return strDate;
}
}
function parseDateTime6(strDate) {
var REGEXP_TO_EXTRACT_DATE = /(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/g,
splitDate = REGEXP_TO_EXTRACT_DATE.exec(strDate);
if (splitDate) {
var date = [splitDate[1], splitDate[2], splitDate[3]].join('-'),
time = [splitDate[4], splitDate[5], splitDate[6]].join(':'),
structuredDate = date + 'T' + time + '+05:30'; // iOS needs format 2020-12-30T23:30:10+05:30 , default parses into GMT time, so need +05:30 for IST conversion
return structuredDate;
} else {
return strDate;
}
}
/**
* isPastDate compares given dates
* @param date1 date to be compared
* @param date2 reference date from which whether date1 is of past is determined
* @return Boolean true/false based on the date calculation
*/
function isPastDate(date1, date2) {
var structuredDate1 = parseDate(date1),
structuredDate2 = date2 ? parseDate(date2) : new Date(currentDateTime);
return new Date(structuredDate2).getTime() > new Date(structuredDate1).getTime();
}
function addDays(sourceDate, days) {
var miliseconds = 24 * 60 * 60 * 1000 * (days || 0),
epochTime = new Date(sourceDate);
return new Date(+epochTime + miliseconds);
}
function getDayName(dateObj) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var inputDate = new Date(dateObj),
dayName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][inputDate.getDay()] || '';
return options.short ? dayName.substr(0, 3) : dayName;
}
function _subtract(value, type) {
var map = {
'days': _subtractDays.bind(this),
'hour': _subtractHours.bind(this),
'seconds': _addSeconds.bind(this)
};
return _samay.call(this, map[type](value));
}
function _subtractDays(days) {
var miliseconds = 24 * 60 * 60 * 1000 * (days || 0),
epochTime = +this.inputDate;
return new Date(+epochTime - miliseconds);
}
function _subtractHours(hours) {
var miliseconds = 60 * 60 * 1000 * (hours || 0),
epochTime = +this.inputDate;
return new Date(+epochTime - miliseconds);
}
function _add(value, type) {
var map = {
'days': _addDays.bind(this),
'hour': _addHours.bind(this),
'seconds': _addSeconds.bind(this)
};
return _samay.call(this, map[type](value));
}
function _addHours(hours) {
var miliseconds = 60 * 60 * 1000 * (hours || 0),
epochTime = +this.inputDate;
return new Date(+epochTime + miliseconds);
}
function _addDays(days) {
var miliseconds = 24 * 60 * 60 * 1000 * (days || 0),
epochTime = +this.inputDate;
return new Date(+epochTime + miliseconds);
}
function _addSeconds(seconds) {
var miliseconds = 1000 * seconds,
epochTime = +this.inputDate;
return new Date(+epochTime + miliseconds);
}
function _getDayName(day) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var dayName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][day] || '';
return options.length ? dayName.substr(0, options.length) : dayName;
}
function _getMonthName(month) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var monthName = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][month] || '';
return options.length ? monthName.substr(0, options.length) : monthName;
}
function _formatMonth(month) {
var updatedMonth = month + 1;
return _prepandZero(updatedMonth);
}
function _formatDate(date) {
return _prepandZero(date);
}
function _prepandZero(value) {
return value < 10 ? '0' + value : value;
}
function _ordinalSuffix(i) {
var j = i % 10,
k = i % 100;
if (j === 1 && k !== 11) {
return i + 'st';
}
if (j === 2 && k !== 12) {
return i + 'nd';
}
if (j === 3 && k !== 13) {
return i + 'rd';
}
return i + 'th';
}
function _format(format) {
var inputDate = this.inputDate,
date = inputDate.getDate(),
month = inputDate.getMonth(),
day = inputDate.getDay(),
// Sunday is 0, Monday is 1 and so on .....
fullYear = inputDate.getFullYear(),
year = fullYear % 100,
hour24 = inputDate.getHours(),
hour12 = hour24 % 12 + (hour24 / 12 === 0 || hour24 / 12 === 1 ? 12 : 0),
minutes = inputDate.getMinutes(),
seconds = inputDate.getSeconds(),
isPM = hour24 >= 12;
return (format || '').replace('mm', _prepandZero(minutes)) // Lower case depicts minutes
.replace('ss', _prepandZero(seconds)).replace('hh', _prepandZero(hour12)) // 'hh' depicts 12 hour format
.replace('HH', _prepandZero(hour24)) // 'HH' stands for the 24 hours format
.replace('h', hour12).replace('H', hour24).replace('A', isPM ? 'PM' : 'AM').replace('ddd', _getDayName(day, { length: 3 })).replace('DD', _formatDate(date)).replace('MMMM', _getMonthName(month)).replace('MMM', _getMonthName(month, { length: 3 })).replace('MM', _formatMonth(month)).replace('yyyy', fullYear).replace('YYYY', fullYear).replace('yy', _prepandZero(year)).replace('YY', _prepandZero(year)).replace('Do', _ordinalSuffix(date)); // If st/nd/rd/th ordinal suffix is requested
}
function _isAfter(refDate) {
var date = this.inputDate;
var refNativeDate = void 0;
if (refDate instanceof Date) {
refNativeDate = refDate;
}
if (refDate && refDate.samayInstance) {
refNativeDate = refDate.originalDate;
}
return +date > +refNativeDate;
}
function _getDaysInMonth() {
var date = this.inputDate,
lastDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
return lastDate.getDate();
}
function _day(dayNumber) {
var day = dayNumber - this.inputDate.getDay();
return _add.call(this, day, 'days');
}
function _samay(inputDate, scanFormat) {
var _fnCallMap;
var formats = _samay.FORMATS,
fnCallMap = (_fnCallMap = {}, _defineProperty(_fnCallMap, formats.DD_MM_YYYY_HH_mm_ss, parseDateTime2), _defineProperty(_fnCallMap, formats.YYYYMMDDHHmm, parseDateTime), _defineProperty(_fnCallMap, formats.YYYYMMDD, parseDate), _defineProperty(_fnCallMap, formats.ddd_DD_MMM_YYYY, parseDateTime3), _defineProperty(_fnCallMap, formats.DD_MM_YYYY, parseDateTime4), _defineProperty(_fnCallMap, formats.MMM_DD_YYYY, parseDateTime5), _defineProperty(_fnCallMap, formats.YYYY_MM_DD_HH_mm_ss, parseDateTime6), _fnCallMap);
if (inputDate instanceof Date) {
this.inputDate = inputDate;
} else if (inputDate && fnCallMap[scanFormat]) {
var structuredDate = fnCallMap[scanFormat](inputDate),
nativeDate = calcDate(structuredDate);
this.inputDate = new Date(nativeDate);
} else if (inputDate && !fnCallMap[scanFormat]) {
this.inputDate = new Date(inputDate);
} else if (!inputDate) {
this.inputDate = new Date();
}
if (this.inputDate === _samay.INVALID) {
throw new SamayError('INVALID DATE - Exception Occurred in constructor');
}
return {
samayInstance: true,
originalDate: this.inputDate,
format: _format.bind(this),
add: _add.bind(this),
subtract: _subtract.bind(this),
isAfter: _isAfter.bind(this),
getDaysInMonth: _getDaysInMonth.bind(this),
day: _day.bind(this)
};
}
_samay.INVALID = 'Invalid Date';
_samay.FORMATS = {
'DD_MM_YYYY_HH_mm_ss': 'DD-MM-YYYY HH:mm:ss',
'YYYYMMDDHHmm': 'YYYYMMDDHHmm',
'YYYYMMDD': 'YYYYMMDD',
'ddd_DD_MMM_YYYY': 'ddd, DD MMM, YYYY',
'DD_MM_YYYY': 'DD/MM/YYYY',
'MMM_DD_YYYY': 'MMM DD, YYYY',
'YYYY_MM_DD_HH_mm_ss': 'YYYY-MM-DD HH:mm:ss'
};
function SamayError(message) {
this.message = message;
this.name = 'Samay Exception';
}
module.exports = {
samay: function samay() {
// Provide fresh context everytime. Context isolation of all instances
return _samay.apply({}, arguments);
},
parseDate: parseDate,
parseDateTime: parseDateTime,
parseDateTime2: parseDateTime2,
isPastDate: isPastDate,
addDays: addDays,
getDayName: getDayName
};