···
1
1
+
{
2
2
+
"editor.defaultFormatter": "denoland.vscode-deno",
3
3
+
"deno.enable": true
4
4
+
}
···
1
1
+
Permission is hereby granted, free of charge, to any person obtaining a copy
2
2
+
of this software and associated documentation files (the "Software"), to deal
3
3
+
in the Software without restriction, including without limitation the rights
4
4
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
5
5
+
copies of the Software, and to permit persons to whom the Software is
6
6
+
furnished to do so, subject to the following conditions:
7
7
+
8
8
+
The above copyright notice and this permission notice shall be included in all
9
9
+
copies or substantial portions of the Software.
10
10
+
11
11
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
12
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
13
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14
14
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15
15
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
16
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17
17
+
SOFTWARE.
···
1
1
+
# date-fns
2
2
+
3
3
+
small date utilities
···
1
1
+
{
2
2
+
"name": "@mary/date-fns",
3
3
+
"version": "0.1.0",
4
4
+
"exports": "./lib/mod.ts",
5
5
+
"fmt": {
6
6
+
"useTabs": true,
7
7
+
"indentWidth": 2,
8
8
+
"lineWidth": 110,
9
9
+
"semiColons": true,
10
10
+
"singleQuote": true
11
11
+
},
12
12
+
"publish": {
13
13
+
"include": ["lib/", "LICENSE", "README.md", "deno.json"]
14
14
+
}
15
15
+
}
···
1
1
+
export type DayOfWeek = 0 | 1 | 2 | 3 | 4 | 5 | 6;
2
2
+
3
3
+
export const getUnixMs = (date: Date): number => {
4
4
+
return date.getTime();
5
5
+
};
6
6
+
7
7
+
export const getMilliseconds = (date: Date): number => {
8
8
+
return date.getMilliseconds();
9
9
+
};
10
10
+
11
11
+
export const getSeconds = (date: Date): number => {
12
12
+
return date.getSeconds();
13
13
+
};
14
14
+
15
15
+
export const getMinutes = (date: Date): number => {
16
16
+
return date.getMinutes();
17
17
+
};
18
18
+
19
19
+
export const getHours = (date: Date): number => {
20
20
+
return date.getHours();
21
21
+
};
22
22
+
23
23
+
export const getDayOfWeek = (date: Date): DayOfWeek => {
24
24
+
return date.getDay() as DayOfWeek;
25
25
+
};
26
26
+
27
27
+
export const getDayOfMonth = (date: Date): number => {
28
28
+
return date.getDate();
29
29
+
};
30
30
+
31
31
+
export const getMonth = (date: Date): number => {
32
32
+
return date.getMonth();
33
33
+
};
34
34
+
35
35
+
export const getYear = (date: Date): number => {
36
36
+
return date.getFullYear();
37
37
+
};
38
38
+
39
39
+
export const isDatetimeBefore = (date: Date, compare: Date): boolean => {
40
40
+
return date !== compare && getUnixMs(date) < getUnixMs(compare);
41
41
+
};
42
42
+
43
43
+
export const isDatetimeAfter = (date: Date, compare: Date): boolean => {
44
44
+
return date !== compare && getUnixMs(date) > getUnixMs(compare);
45
45
+
};
46
46
+
47
47
+
export const isDatetimeSame = (a: Date, b: Date): boolean => {
48
48
+
return a === b || getUnixMs(a) === getUnixMs(b);
49
49
+
};
50
50
+
51
51
+
export const isDateSame = (a: Date, b: Date): boolean => {
52
52
+
if (a === b) {
53
53
+
return true;
54
54
+
}
55
55
+
56
56
+
return getDayOfMonth(a) === getDayOfMonth(b) && getMonth(a) === getMonth(b) && getYear(a) === getYear(b);
57
57
+
};
58
58
+
59
59
+
export const isMonthSame = (a: Date, b: Date): boolean => {
60
60
+
if (a === b) {
61
61
+
return true;
62
62
+
}
63
63
+
64
64
+
return getMonth(a) === getMonth(b) && getYear(a) === getYear(b);
65
65
+
};
66
66
+
67
67
+
export const isYearSame = (a: Date, b: Date): boolean => {
68
68
+
if (a === b) {
69
69
+
return true;
70
70
+
}
71
71
+
72
72
+
return getYear(a) === getYear(b);
73
73
+
};
74
74
+
75
75
+
export const min = (a: Date, b: Date): Date => {
76
76
+
return isDatetimeBefore(a, b) ? a : b;
77
77
+
};
78
78
+
79
79
+
export const max = (a: Date, b: Date): Date => {
80
80
+
return isDatetimeAfter(a, b) ? a : b;
81
81
+
};
82
82
+
83
83
+
export const clamp = (date: Date, min: Date | undefined, max: Date | undefined): Date => {
84
84
+
if (min !== undefined && isDatetimeBefore(date, min)) {
85
85
+
return min;
86
86
+
}
87
87
+
88
88
+
if (max !== undefined && isDatetimeAfter(date, max)) {
89
89
+
return max;
90
90
+
}
91
91
+
92
92
+
return date;
93
93
+
};
94
94
+
95
95
+
export const cloneDate = (date: Date): Date => {
96
96
+
return new Date(date);
97
97
+
};
98
98
+
99
99
+
export const startOfDay = (date: Date): Date => {
100
100
+
const d = cloneDate(date);
101
101
+
102
102
+
d.setHours(0, 0, 0, 0);
103
103
+
return d;
104
104
+
};
105
105
+
106
106
+
export const endOfDay = (date: Date): Date => {
107
107
+
const d = cloneDate(date);
108
108
+
109
109
+
d.setHours(23, 59, 59, 999);
110
110
+
return d;
111
111
+
};
112
112
+
113
113
+
export const startOfWeek = (date: Date): Date => {
114
114
+
const d = cloneDate(date);
115
115
+
116
116
+
d.setDate(getDayOfMonth(d) - getDayOfWeek(d));
117
117
+
d.setHours(0, 0, 0, 0);
118
118
+
return d;
119
119
+
};
120
120
+
121
121
+
export const endOfWeek = (date: Date): Date => {
122
122
+
const d = cloneDate(date);
123
123
+
124
124
+
d.setDate(getDayOfMonth(d) + (6 - getDayOfWeek(d)));
125
125
+
d.setHours(23, 59, 59, 999);
126
126
+
return d;
127
127
+
};
128
128
+
129
129
+
export const startOfMonth = (date: Date): Date => {
130
130
+
const d = cloneDate(date);
131
131
+
132
132
+
d.setDate(1);
133
133
+
d.setHours(0, 0, 0, 0);
134
134
+
return d;
135
135
+
};
136
136
+
137
137
+
export const endOfMonth = (date: Date): Date => {
138
138
+
const d = cloneDate(date);
139
139
+
140
140
+
d.setMonth(getMonth(d) + 1, 0);
141
141
+
d.setHours(23, 59, 59, 999);
142
142
+
return d;
143
143
+
};
144
144
+
145
145
+
export const startOfYear = (date: Date): Date => {
146
146
+
const d = cloneDate(date);
147
147
+
148
148
+
d.setMonth(0, 1);
149
149
+
d.setHours(0, 0, 0, 0);
150
150
+
return d;
151
151
+
};
152
152
+
153
153
+
export const endOfYear = (date: Date): Date => {
154
154
+
const d = cloneDate(date);
155
155
+
156
156
+
d.setMonth(11, 31);
157
157
+
d.setHours(23, 59, 59, 999);
158
158
+
return d;
159
159
+
};
160
160
+
161
161
+
export const addMilliseconds = (date: Date, milliseconds: number): Date => {
162
162
+
const d = cloneDate(date);
163
163
+
164
164
+
d.setMilliseconds(getMilliseconds(d) + milliseconds);
165
165
+
return d;
166
166
+
};
167
167
+
168
168
+
export const addSeconds = (date: Date, seconds: number): Date => {
169
169
+
const d = cloneDate(date);
170
170
+
171
171
+
d.setSeconds(getSeconds(d) + seconds);
172
172
+
return d;
173
173
+
};
174
174
+
175
175
+
export const addMinutes = (date: Date, minutes: number): Date => {
176
176
+
const d = cloneDate(date);
177
177
+
178
178
+
d.setMinutes(getMinutes(d) + minutes);
179
179
+
return d;
180
180
+
};
181
181
+
182
182
+
export const addHours = (date: Date, hours: number): Date => {
183
183
+
const d = cloneDate(date);
184
184
+
185
185
+
d.setHours(getHours(d) + hours);
186
186
+
return d;
187
187
+
};
188
188
+
189
189
+
export const addDays = (date: Date, days: number): Date => {
190
190
+
const d = cloneDate(date);
191
191
+
192
192
+
d.setDate(getDayOfMonth(d) + days);
193
193
+
return d;
194
194
+
};
195
195
+
196
196
+
export const addMonths = (date: Date, months: number): Date => {
197
197
+
const d = cloneDate(date);
198
198
+
199
199
+
d.setMonth(getMonth(d) + months);
200
200
+
return d;
201
201
+
};
202
202
+
203
203
+
export const addYears = (date: Date, years: number): Date => {
204
204
+
const d = cloneDate(date);
205
205
+
206
206
+
d.setFullYear(getYear(d) + years);
207
207
+
return d;
208
208
+
};
209
209
+
210
210
+
export const previousDay = (date: Date, day: DayOfWeek): Date => {
211
211
+
let delta = day - getDayOfWeek(date);
212
212
+
if (delta >= 0) {
213
213
+
delta -= 7;
214
214
+
}
215
215
+
216
216
+
return addDays(date, delta);
217
217
+
};
218
218
+
219
219
+
export const nextDay = (date: Date, day: DayOfWeek): Date => {
220
220
+
let delta = day - getDayOfWeek(date);
221
221
+
if (delta <= 0) {
222
222
+
delta += 7;
223
223
+
}
224
224
+
225
225
+
return addDays(date, delta);
226
226
+
};