-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
177 lines (137 loc) · 5.25 KB
/
Copy pathindex.js
File metadata and controls
177 lines (137 loc) · 5.25 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
class Datex extends Date {
constructor(...args) {
super(...args);
}
startOfDay() {
return new Datex(this.getFullYear(), this.getMonth(), this.getDate());
}
endOfDay() {
return new Datex(this.getFullYear(), this.getMonth(), this.getDate(), 23, 59, 59, 999);
}
isLeapYear() {
const year = this.getFullYear();
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
// returns the days in month for the created date object
daysInMonth() {
const month = this.getMonth();
const year = this.getFullYear();
return new Date(year, month + 1, 0).getDate();
}
// compares two dates
isSameDayWith(date) {
if(!(date instanceof Date) || !(date instanceof Datex)){
throw new Error(`${date} given date object is not valid. Please give an instance of built-in Date object or Datex object.`)
}
return this.getFullYear() === date.getFullYear() &&
this.getMonth() === date.getMonth() &&
this.getDate() === date.getDate();
}
// returns True if the created date is today
isToday() {
const today = new Datex();
return this.isSameDayWith(today);
}
// returns MySQL Date string
toMySQLDateString() {
return this.getUTCFullYear() + "-" + (this.getUTCMonth() + 1) + "-" + this.getUTCDate();
}
// returns MySQL DateTime string
toMySQLDateTimeString() {
return this.toISOString().slice(0, 19).replace('T', ' ');
}
// returns a date range for today for example : "created_at BETWEEN '2023-04-01' AND '2023-04-01'"
dateRangeForToday(columnName) {
const startOfDayDate = this.startOfDay().toMySQLDateString();
const endOfDayDate = this.endOfDay().toMySQLDateString();
return `${columnName} BETWEEN '${startOfDayDate}' AND '${endOfDayDate}'`;
}
// takes a new date as an input and returns the relative time respect to now.
relativeTime(date) {
if(!(date instanceof Date) || !(date instanceof Datex)){
throw new Error(`${date} given date object is not valid. Please give an instance of built-in Date object or Datex object.`)
}
const now = new Date();
const msPerMinute = 60 * 1000;
const msPerHour = msPerMinute * 60;
const msPerDay = msPerHour * 24;
const msPerMonth = msPerDay * 30;
const msPerYear = msPerDay * 365;
const elapsed = now - date;
if (elapsed < msPerMinute) {
return Math.round(elapsed / 1000) + ' seconds ago';
} else if (elapsed < msPerHour) {
return Math.round(elapsed / msPerMinute) + ' minutes ago';
} else if (elapsed < msPerDay) {
return Math.round(elapsed / msPerHour) + ' hours ago';
} else if (elapsed < msPerMonth) {
return Math.round(elapsed / msPerDay) + ' days ago';
} else if (elapsed < msPerYear) {
return Math.round(elapsed / msPerMonth) + ' months ago';
} else {
return Math.round(elapsed / msPerYear) + ' years ago';
}
}
isWeekend() {
const dayOfWeek = this.getDay();
return dayOfWeek === 0 || dayOfWeek === 6;
}
isWeekday() {
return !this.isWeekend();
}
// returns the difference as a day
differenceInDays(date) {
if(!(date instanceof Date) || !(date instanceof Datex)){
throw new Error(`${date} given date object is not valid. Please give an instance of built-in Date object or Datex object.`)
}
const msPerDay = 24 * 60 * 60 * 1000;
const date1 = this.startOfDay();
const date2 = date.startOfDay();
return Math.round((date2 - date1) / msPerDay);
}
// checks two dates are in the same month
isSameMonth(date) {
if(!(date instanceof Date) || !(date instanceof Datex)){
throw new Error(`${date} given date object is not valid. Please give an instance of built-in Date object or Datex object.`)
}
return this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth();
}
// DATE MANUPILATION
// addMilliseconds(milliseconds) {
// const result = new Datex(this);
// result.setMilliseconds(this.getMilliseconds() + milliseconds);
// return result;
// }
addSeconds(seconds) {
const result = new Datex(this);
result.setSeconds(this.getSeconds() + seconds);
return result;
}
addMinutes(minutes) {
const result = new Datex(this);
result.setMinutes(this.getMinutes() + minutes);
return result;
}
addHours(hours) {
const result = new Datex(this);
result.setHours(this.getHours() + hours);
return result;
}
addDays(days) {
const result = new Datex(this);
result.setDate(this.getDate() + days);
return result;
}
addMonths(months) {
const result = new Datex(this);
const currentMonth = this.getMonth()
result.setMonth(currentMonth + months);
return result;
}
addYears(years) {
const result = new Datex(this);
result.setFullYear(this.getFullYear() + years);
return result;
}
}
module.exports = Datex;