-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.js
More file actions
182 lines (162 loc) · 4.06 KB
/
Copy pathdate.js
File metadata and controls
182 lines (162 loc) · 4.06 KB
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
// Helper function
const isNumber = (value) => !Number.isNaN(parseFloat(value));
// Adding time intervals by milliseconds for easy usage
Date.convertTable = {
second: 1000,
};
Date.convertTable.minute = Date.convertTable.second * 60;
Date.convertTable.hour = Date.convertTable.minute * 60;
Date.convertTable.day = Date.convertTable.hour * 24;
Date.convertTable.week = Date.convertTable.day * 7;
Date.convertTable.year = Date.convertTable.day * 365;
/**
* Add a cretin unit of time the the date
* @param {String} unit The unit of time to add, options:
* * seconds
* * minutes
* * hours
* * days
* * weeks
* * months
* * years
* @param {Number} value The value to add
*/
Date.prototype.add = function (unit, value) {
if (!isNumber(value)) {
throw new Error('Invalid value, must be a number');
}
// Converting the time unit to lowercase
unit = unit.toLowerCase();
// Force 's' in the end of the string to convert single to plural
if (!unit.endsWith('s')) {
unit += 's';
}
// Add by the unit
if (unit === 'seconds') {
this.setTime(this.getTime() + Date.convertTable.second * value);
} else if (unit === 'minutes') {
this.setTime(this.getTime() + Date.convertTable.minute * value);
} else if (unit === 'hours') {
this.setTime(this.getTime() + Date.convertTable.hour * value);
} else if (unit === 'days') {
this.setTime(this.getTime() + Date.convertTable.day * value);
} else if (unit === 'weeks') {
this.setTime(this.getTime() + Date.convertTable.week * value);
} else if (unit === 'months') {
this.setUTCMonth(this.getMonth() + value);
} else if (unit === 'years') {
this.setUTCFullYear(this.getFullYear() + value);
} else {
throw new Error('Invalid date unit');
}
};
/**
* Subtract a cretin unit of time the the date
* @param {String} unit The unit of time to add
* * seconds
* * minutes
* * hours
* * days
* * weeks
* * months
* * years
* @param {Number} value The value to add
*/
Date.prototype.subtract = function (unit, value) {
this.add(unit, -value);
};
/**
* Change date to the next day
*/
Date.prototype.nextDay = function () {
this.add('days', 1);
};
/**
* Change date to the next week
*/
Date.prototype.nextWeek = function () {
this.add('weeks', 1);
};
/**
* Change date to the next month
*/
Date.prototype.nextMonth = function () {
this.add('months', 1);
};
/**
* Change date to the next year
*/
Date.prototype.nextYear = function () {
this.add('years', 1);
};
/**
* Change date to the previous day
*/
Date.prototype.prevDay = function () {
this.subtract('days', 1);
};
/**
* Change date to the previous week
*/
Date.prototype.prevWeek = function () {
this.subtract('weeks', 1);
};
/**
* Change date to the previous month
*/
Date.prototype.prevMonth = function () {
this.subtract('months', 1);
};
/**
* Change date to the previous year
*/
Date.prototype.prevYear = function () {
this.subtract('years', 1);
};
/**
* Change date to the end of the day (23:59:59)
*/
Date.prototype.endOfDay = function () {
this.setUTCMinutes(59);
this.setUTCSeconds(59);
this.setUTCHours(23);
};
/**
* Change date to the end of the month and end of the day
*/
Date.prototype.endOfMonth = function () {
this.setUTCMonth(this.getUTCMonth() + 1);
this.setUTCDate(0);
this.endOfDay();
};
/**
* Change date to the end of the year and end of the day
*/
Date.prototype.endOfYear = function () {
this.setUTCMonth(12);
this.setUTCDate(0);
this.endOfDay();
};
/**
* Change date to the start of the day (00:00:00)
*/
Date.prototype.startOfDay = function () {
this.setUTCMinutes(0);
this.setUTCSeconds(0);
this.setUTCHours(0);
};
/**
* Change date to the start of the month and start of the day
*/
Date.prototype.startOfMonth = function () {
this.setUTCDate(1);
this.startOfDay();
};
/**
* Change date to the start of the year and start of the day
*/
Date.prototype.startOfYear = function () {
this.setUTCMonth(0);
this.setUTCDate(1);
this.startOfDay();
};