-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateExtended.js
400 lines (361 loc) · 12 KB
/
DateExtended.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
class NumberHelper {
/**
* Prefixes a zero in the given number if it is in the range: 0-9
* @param {Number} number
*/
static prefixZero(number) {
const numberStr = number.toString();
return numberStr.length === 1 ? "0" + numberStr : numberStr;
}
}
const shortWeekDayNames = [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
];
const fullWeekDayNames = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const shortMonthNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const fullMonthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
// TODO:
// find a unique name for npmjs.com
// like: datetime-extended, new-date-extended
// how to minify code.
// write tests.
class DateExtended extends Date {
// class private variable:
// #ONE_MINUTE_IN_MILLISECONDS = 60 * 1000;
// #ONE_HOUR_IN_MILLISECONDS = 60 * 60 * 1000;
// #ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
constructor() {
if (arguments[0] === undefined) {
super();
}
else {
super(...arguments);
}
this.ONE_MINUTE_IN_MILLISECONDS = 60 * 1000;
this.ONE_HOUR_IN_MILLISECONDS = 60 * 60 * 1000;
this.ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
}
/**
* @returns {Number} Total number of passed milliseconds up-till now, expressed in whole and fractional number.
*/
totalMilliseconds() { return new Date().getTime() - this.getTime(); }
/**
* @returns {Number} Total number of passed seconds up-till now, expressed in whole and fractional number.
*/
totalSeconds() { return this.totalMilliseconds() / 1000; }
/**
* @returns {Number} Total number of passed minutes up-till now, expressed in whole and fractional number.
*/
totalMinutes() { return this.totalSeconds() / 60; }
/**
* @returns {Number} Total number of passed hours up-till now, expressed in whole and fractional number.
*/
totalHours() { return this.totalMinutes() / 60; }
/**
* @returns {Number} Total number of passed days up-till now, expressed in whole and fractional number.
*/
totalDays() { return this.totalHours() / 24; }
/**
* @param {Number} year - Full year for the month.
* @param {Number} month - Zero based month number (0-11)
* @returns {Number} Total number of days of the month.
*/
getDaysInMonth(year, month) {
// source: https://stackoverflow.com/a/222439
return new Date(year, month + 1, 0).getDate();
}
/**
* Adds/Subtracts given number of years in this instance and returns a new instance.
* @param {Number} years - Positive or Negative number of years.
* @returns {DateExtended}
*/
addYears(years) {
const resultantYear = this.getFullYear() + years;
// source: https://stackoverflow.com/a/43794682
if (resultantYear > 275760 || resultantYear < -271821) {
throw "Invalid Date: Out of Range";
}
const thisMonth = this.getMonth();
let thisDate = this.getDate();
if (thisMonth === 1 /* if its Feb */ && thisDate === 29) {
thisDate = this.getDaysInMonth(resultantYear, thisMonth);
}
return new DateExtended(
resultantYear,
thisMonth,
thisDate,
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds()
);
}
/**
* Adds/Subtracts given number of months in this instance and returns a new instance.
* @param {Number} months - Positive or Negative number of months.
* @returns {DateExtended}
*/
addMonths(months) {
// source: https://stackoverflow.com/a/11500017
const thisYear = this.getFullYear();
const thisMonth = this.getMonth();
const resultantYear = Math.floor(((thisYear * 12) + thisMonth + months) / 12);
// source: https://stackoverflow.com/a/43794682
if (resultantYear > 275760 || resultantYear < -271821) {
throw "Invalid Date: Out of Range";
}
const resultantMonth = ((thisYear * 12) + thisMonth + months) % 12;
let thisDate = this.getDate();
if (thisDate > 28) {
const resultantDate = this.getDaysInMonth(resultantYear, resultantMonth);
if (resultantDate < thisDate) {
thisDate = resultantDate;
}
}
return new DateExtended(
resultantYear,
resultantMonth,
thisDate,
this.getHours(),
this.getMinutes(),
this.getSeconds(),
this.getMilliseconds()
);
}
/**
* Adds/Subtracts given number of days in this instance and returns a new instance.
* @param {Number} days - Positive or Negative number of days.
* @returns {DateExtended}
*/
addDays(days) {
return new DateExtended(this.getTime() + (days * this.ONE_DAY_IN_MILLISECONDS));
}
/**
* Adds/Subtracts given number of hours in this instance and returns a new instance.
* @param {Number} hours - Positive or Negative number of hours.
* @returns {DateExtended}
*/
addHours(hours) {
return new DateExtended(this.getTime() + (hours * this.ONE_HOUR_IN_MILLISECONDS));
}
/**
* Adds/Subtracts given number of minutes in this instance and returns a new instance.
* @param {Number} minutes - Positive or Negative number of minutes.
* @returns {DateExtended}
*/
addMinutes(minutes) {
return new DateExtended(this.getTime() + (minutes * this.ONE_MINUTE_IN_MILLISECONDS));
}
/**
* Adds/Subtracts given number of seconds in this instance and returns a new instance.
* @param {Number} seconds - Positive or Negative number of seconds.
* @returns {DateExtended}
*/
addSeconds(seconds) {
return new DateExtended(this.getTime() + (seconds * 1000));
}
/**
* Adds/Subtracts given number of milliseconds in this instance and returns a new instance.
* @param {Number} milliseconds - Positive or Negative number of milliseconds.
* @returns {DateExtended}
*/
addMilliseconds(milliseconds) {
return new DateExtended(this.getTime() + milliseconds);
}
getHoursOf12HourFormat() {
const hours = this.getHours();
if (hours == 0) {
return 12;
}
if (hours > 12) {
return hours - 12;
}
return hours;
}
getAmOrPmInShort() {
if (this.getHours() < 13) {
return "A";
}
return "P";
}
getAmOrPmInFull() {
if (this.getHours() < 13) {
return "AM";
}
return "PM";
}
/**
* Every representative must be separated by a non-word character.
*
* Non-word characters: ~!@#$%^&*()+`-=[]\;',./{}|:"<>? or a space.
*
* d -> Represents the day of the month as a number from 1 through 31.
*
* dd -> Represents the day of the month as a number from 01 through 31.
*
* ddd-> Represents the abbreviated name of the day (Mon, Tues, Wed, etc).
*
* dddd-> Represents the full name of the day (Monday, Tuesday, etc).
*
* h-> 12-hour clock hour (e.g. 4).
*
* hh-> 12-hour clock, with a leading 0 (e.g. 06).
*
* H-> 24-hour clock hour (e.g. 15)
*
* HH-> 24-hour clock hour, with a leading 0 (e.g. 22).
*
* m-> Minutes
*
* mm-> Minutes with a leading zero.
*
* M-> Month number (eg.3)
*
* MM-> Month number with leading zero(eg.04)
*
* MMM-> Abbreviated Month Name (e.g. Dec)
*
* MMMM-> Full month name (e.g. December)
*
* s-> Seconds
*
* ss-> Seconds with leading zero.
*
* t-> Abbreviated AM / PM (e.g. A or P)
*
* tt-> AM / PM (e.g. AM or PM)
*
* yyy-> Year, (e.g. 2015)
*
* yyyy-> Year, (e.g. 2015)
*
* Source: https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1
*
* @param {String} stringToFormat
* @returns {String} Formatted date string.
*/
format(stringToFormat) {
if (/\byyyy\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\byyyy\b/g, this.getFullYear());
}
if (/\byyy\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\byyy\b/g, this.getFullYear());
}
if (/\bHH\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bHH\b/g, NumberHelper.prefixZero(this.getHours()));
}
if (/\bH\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bH\b/g, this.getHours());
}
if (/\bhh\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bhh\b/g, NumberHelper.prefixZero(this.getHoursOf12HourFormat()));
}
if (/\bh\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bh\b/g, this.getHoursOf12HourFormat());
}
if (/\bmm\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bmm\b/g, NumberHelper.prefixZero(this.getMinutes()));
}
if (/\bm\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bm\b/g, this.getMinutes());
}
if (/\bss\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bss\b/g, NumberHelper.prefixZero(this.getSeconds()));
}
if (/\bs\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bs\b/g, this.getSeconds());
}
if (/\bdddd\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bdddd\b/g, fullWeekDayNames[this.getDay()]);
}
if (/\bddd\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bddd\b/g, shortWeekDayNames[this.getDay()]);
}
if (/\bdd\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bdd\b/g, NumberHelper.prefixZero(this.getDate()));
}
if (/\bd\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bd\b/g, this.getDate());
}
if (/\bMMMM\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bMMMM\b/g, fullMonthNames[this.getMonth()]);
}
if (/\bMMM\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bMMM\b/g, shortMonthNames[this.getMonth()]);
}
if (/\bMM\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bMM\b/g, NumberHelper.prefixZero(this.getMonth() + 1));
}
if (/\bM\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bM\b/g, this.getMonth() + 1);
}
if (/\btt\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\btt\b/g, this.getAmOrPmInFull());
}
if (/\bt\b/g.test(stringToFormat)) {
stringToFormat = stringToFormat.replace(/\bt\b/g, this.getAmOrPmInShort());
}
return stringToFormat;
}
}
// console.log(new DateExtended(1000).totalMilliseconds());
// console.log(new DateExtended("2020-10-31T00:12:12").format('d dd ddd dddd h hh H HH m mm s ss M MM MMM MMMM t tt yyy yyyy'));
// console.log(new DateExtended("2020-10-01T13:12:12").format('d dd ddd dddd h hh H HH m mm s ss M MM MMM MMMM t tt yyy yyyy'));
// console.log(new DateExtended("2020-10-31T00:12:12").format('dd-mm-yyy'));
// console.log(new DateExtended("2020-10-31T00:12:12").format('dd/mm/yyy'));
// console.log(new DateExtended("2020-10-31T00:12:12").format('dd:mm:yyy'));
// console.log(new DateExtended("2020-10-31T00:12:12").format('dd!mm!yyy'));
// console.log(new DateExtended("2020-10-31T00:12:12").format("dd'mm'yyy"));
// console.log(new DateExtended("2020-10-31T00:12:12").format("dd`mm`yyy"));
// this case wont work because an underscore is a word-character, we need to separate them with a non-word character.
// console.log(new DateExtended("2020-10-31T00:12:12").format('dd_mm_yyy'));
// console.log(new DateExtended().addDays(1));
// console.log(new DateExtended().addHours(1));
// console.log(new DateExtended().addHours(-1));
// console.log(new DateExtended().addMinutes(1));
// console.log(new DateExtended().addMinutes(-1));
// console.log(new DateExtended().addSeconds(1));
// console.log(new DateExtended().addSeconds(-1));
// console.log(new Date(2020, 1, 31, 00, 12, 12).addMonths(0).format("dd-MM-MMM-yyy T HH mm ss"));
// console.log(new DateExtended(2020, 0, 29, 00, 12, 12).addMonths(1).format("dd-(MM-MMM)-yyyy"));
// console.log(new DateExtended(2020, 1, 29, 00, 12, 12).addYears(1).format("dd-MMM-yyyy"));