scdate 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/internal/SDate.d.ts +3 -0
- package/dist/internal/SDate.js +3 -0
- package/dist/internal/STime.d.ts +3 -0
- package/dist/internal/STime.js +3 -0
- package/dist/internal/STimestamp.d.ts +3 -0
- package/dist/internal/STimestamp.js +3 -0
- package/dist/internal/SWeekdays.d.ts +7 -0
- package/dist/internal/SWeekdays.js +7 -0
- package/dist/internal/date.js +1 -0
- package/dist/internal/time.js +2 -0
- package/dist/internal/utils.d.ts +4 -0
- package/dist/internal/utils.js +4 -0
- package/dist/internal/zoned.d.ts +4 -0
- package/dist/internal/zoned.js +4 -0
- package/dist/sDate.d.ts +280 -0
- package/dist/sDate.js +283 -0
- package/dist/sTime.d.ts +131 -0
- package/dist/sTime.js +131 -0
- package/dist/sTimestamp.d.ts +266 -0
- package/dist/sTimestamp.js +266 -0
- package/dist/sWeekdays.d.ts +116 -0
- package/dist/sWeekdays.js +118 -0
- package/package.json +8 -8
package/dist/sDate.js
CHANGED
|
@@ -4,42 +4,91 @@ import { getDateAsUTCDateMini, getISODateFromISODate, getISODateFromZonedDate, g
|
|
|
4
4
|
import { getAtIndex } from './internal/utils.js';
|
|
5
5
|
import { getIndexForWeekday } from './internal/weekdays.js';
|
|
6
6
|
import { getMillisecondsInUTCFromDate, getTimeZonedDate, } from './internal/zoned.js';
|
|
7
|
+
/**
|
|
8
|
+
* --- Factory ---
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Returns a new SDate instance.
|
|
12
|
+
*
|
|
13
|
+
* @param date An instance of SDate or a string in the YYYY-MM-DD format.
|
|
14
|
+
*/
|
|
7
15
|
export const sDate = (date) => {
|
|
8
16
|
if (date instanceof SDate) {
|
|
9
17
|
return date;
|
|
10
18
|
}
|
|
11
19
|
return new SDate(date);
|
|
12
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* --- Factory helpers ---
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Returns a new SDate instance with the current date in the given time zone.
|
|
26
|
+
*
|
|
27
|
+
* @param timeZone The time zone to get the current date for. See
|
|
28
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
29
|
+
*/
|
|
13
30
|
export const getDateToday = (timeZone) => {
|
|
14
31
|
const date = getTimeZonedDate(Date.now(), timeZone);
|
|
15
32
|
return sDate(getISODateFromZonedDate(date));
|
|
16
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* Returns a new SDate instance set to the next date after the provided date
|
|
36
|
+
* that match the given weekday.
|
|
37
|
+
*
|
|
38
|
+
* @param date The date to start from (not included). It can be an SDate or a
|
|
39
|
+
* string in the YYYY-MM-DD format.
|
|
40
|
+
* @param weekday The weekday to find the next date for.
|
|
41
|
+
*/
|
|
17
42
|
export const getNextDateByWeekday = (date, weekday) => {
|
|
18
43
|
const sDateValue = sDate(date);
|
|
19
44
|
const weekdayIndex = getIndexForWeekday(weekday);
|
|
20
45
|
const todaysWeekdayIndex = DayToWeekday.indexOf(getWeekdayFromDate(sDateValue));
|
|
46
|
+
// Find the adjustment to get the next date that matches `weekday`
|
|
21
47
|
let adjustment = weekdayIndex - todaysWeekdayIndex;
|
|
22
48
|
if (adjustment <= 0) {
|
|
23
49
|
adjustment += DaysInWeek;
|
|
24
50
|
}
|
|
25
51
|
return addDaysToDate(sDateValue, adjustment);
|
|
26
52
|
};
|
|
53
|
+
/**
|
|
54
|
+
* Returns a new SDate instance set to date that is before the provided date and
|
|
55
|
+
* matches the given weekday.
|
|
56
|
+
*
|
|
57
|
+
* @param date The date to start from (not included).
|
|
58
|
+
* @param weekday The weekday to find the previous date for. It can be an SDate
|
|
59
|
+
* or a string in the YYYY-MM-DD format.
|
|
60
|
+
*/
|
|
27
61
|
export const getPreviousDateByWeekday = (date, weekday) => {
|
|
28
62
|
const sDateValue = sDate(date);
|
|
29
63
|
const weekdayIndex = getIndexForWeekday(weekday);
|
|
30
64
|
const todaysWeekdayIndex = DayToWeekday.indexOf(getWeekdayFromDate(sDateValue));
|
|
65
|
+
// Find the adjustment to get the previous date that matches `weekday`
|
|
31
66
|
let adjustment = weekdayIndex - todaysWeekdayIndex;
|
|
32
67
|
if (adjustment >= 0) {
|
|
33
68
|
adjustment -= DaysInWeek;
|
|
34
69
|
}
|
|
35
70
|
return addDaysToDate(sDateValue, adjustment);
|
|
36
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* Returns a new SDate instance set to the first day of the month for the
|
|
74
|
+
* provided date.
|
|
75
|
+
*
|
|
76
|
+
* @param date The date to get the first day of the month for. It can be an
|
|
77
|
+
* SDate or a string in the YYYY-MM-DD format.
|
|
78
|
+
*/
|
|
37
79
|
export const getDateForFirstDayOfMonth = (date) => {
|
|
38
80
|
const sDateValue = sDate(date);
|
|
39
81
|
const nativeDate = getDateAsUTCDateMini(sDateValue);
|
|
40
82
|
nativeDate.setDate(1);
|
|
41
83
|
return sDate(getISODateFromZonedDate(nativeDate));
|
|
42
84
|
};
|
|
85
|
+
/**
|
|
86
|
+
* Returns a new SDate instance set to the last day of the month for the
|
|
87
|
+
* provided date.
|
|
88
|
+
*
|
|
89
|
+
* @param date The date to get the last day of the month for. It can be an SDate
|
|
90
|
+
* or a string in the YYYY-MM-DD format.
|
|
91
|
+
*/
|
|
43
92
|
export const getDateForLastDayOfMonth = (date) => {
|
|
44
93
|
const sDateValue = sDate(date);
|
|
45
94
|
const nativeDate = getDateAsUTCDateMini(sDateValue);
|
|
@@ -47,29 +96,79 @@ export const getDateForLastDayOfMonth = (date) => {
|
|
|
47
96
|
nativeDate.setHours(0, 0, 0, 0);
|
|
48
97
|
return sDate(getISODateFromZonedDate(nativeDate));
|
|
49
98
|
};
|
|
99
|
+
/**
|
|
100
|
+
* --- Getters ---
|
|
101
|
+
*/
|
|
102
|
+
/**
|
|
103
|
+
* Returns the year from the given date.
|
|
104
|
+
*
|
|
105
|
+
* @param date The date to get the year from. It can be an SDate or a string in
|
|
106
|
+
* the YYYY-MM-DD format.
|
|
107
|
+
*/
|
|
50
108
|
export const getYearFromDate = (date) => {
|
|
51
109
|
const sDateValue = sDate(date);
|
|
52
110
|
return Number(getISOYearFromISODate(sDateValue.date));
|
|
53
111
|
};
|
|
112
|
+
/**
|
|
113
|
+
* Returns the month from the given date. Returns a 0-index value (i.e. Janary
|
|
114
|
+
* is 0 and December is 11) to match the result from native Date object.
|
|
115
|
+
*
|
|
116
|
+
* @param date The date to get the month from. It can be an SDate or a string in
|
|
117
|
+
* the YYYY-MM-DD format.
|
|
118
|
+
*/
|
|
54
119
|
export const getMonthFromDate = (date) => {
|
|
55
120
|
const sDateValue = sDate(date);
|
|
56
121
|
return Number(getISOMonthFromISODate(sDateValue.date)) - 1;
|
|
57
122
|
};
|
|
123
|
+
/**
|
|
124
|
+
* Returns the day of the month from the given date.
|
|
125
|
+
*
|
|
126
|
+
* @param date The date to get the day from. It can be an SDate or a string in
|
|
127
|
+
* the YYYY-MM-DD format.
|
|
128
|
+
*/
|
|
58
129
|
export const getDateFromDate = (date) => {
|
|
59
130
|
const sDateValue = sDate(date);
|
|
60
131
|
return Number(getISODateFromISODate(sDateValue.date));
|
|
61
132
|
};
|
|
133
|
+
/**
|
|
134
|
+
* Returns the day of the week from the given date (Sunday to Saturday / 0 to
|
|
135
|
+
* 6).
|
|
136
|
+
*
|
|
137
|
+
* @param date The date to get the weekday from. It can be an SDate or a string
|
|
138
|
+
* in the YYYY-MM-DD format.
|
|
139
|
+
*/
|
|
62
140
|
export const getWeekdayFromDate = (date) => {
|
|
63
141
|
const sDateValue = sDate(date);
|
|
64
142
|
const nativeDate = getDateAsUTCDateMini(sDateValue);
|
|
65
143
|
return getAtIndex(DayToWeekday, nativeDate.getDay());
|
|
66
144
|
};
|
|
145
|
+
/**
|
|
146
|
+
* Returns a native Date adjusted so that the local time of that date matches
|
|
147
|
+
* the local time at the specified time zone.
|
|
148
|
+
*
|
|
149
|
+
* @param date The date to get the time zoned date from. It can be an SDate or a
|
|
150
|
+
* string in the YYYY-MM-DD format.
|
|
151
|
+
* @param timeZone The time zone to adjust the date to. See
|
|
152
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
153
|
+
*/
|
|
67
154
|
export const getTimeZonedDateFromDate = (date, timeZone) => {
|
|
68
155
|
const sDateValue = sDate(date);
|
|
69
156
|
const milliseconds = getMillisecondsInUTCFromDate(sDateValue, timeZone);
|
|
70
157
|
const zonedTime = getTimeZonedDate(milliseconds, timeZone);
|
|
71
158
|
return zonedTime;
|
|
72
159
|
};
|
|
160
|
+
/**
|
|
161
|
+
* Returns the number of days between the first date to the second date. The
|
|
162
|
+
* value is positive if the first date is before the second date, and negative
|
|
163
|
+
* if the first date is after the second date. This accounts for calendar days
|
|
164
|
+
* and not full 24-hour periods which could be different due to daylight saving
|
|
165
|
+
* adjustments.
|
|
166
|
+
*
|
|
167
|
+
* @param date1 The first date to get the days between. It can be an SDate or a
|
|
168
|
+
* string in the YYYY-MM-DD format.
|
|
169
|
+
* @param date2 The second date to get the days between. It can be an SDate or a
|
|
170
|
+
* string in the YYYY-MM-DD format.
|
|
171
|
+
*/
|
|
73
172
|
export const getDaysBetweenDates = (date1, date2) => {
|
|
74
173
|
const sDate1 = sDate(date1);
|
|
75
174
|
const sDate2 = sDate(date2);
|
|
@@ -77,6 +176,26 @@ export const getDaysBetweenDates = (date1, date2) => {
|
|
|
77
176
|
const ms2 = getDateAsUTCDateMini(sDate2).getTime();
|
|
78
177
|
return Math.round((ms2 - ms1) / MillisecondsInDay);
|
|
79
178
|
};
|
|
179
|
+
/**
|
|
180
|
+
* Returns a string representation that includes all of the date components of
|
|
181
|
+
* the given date formatted according to the given locale.
|
|
182
|
+
*
|
|
183
|
+
* @param date The date to get the full string representation for. It can be an
|
|
184
|
+
* SDate or a string in the YYYY-MM-DD format.
|
|
185
|
+
* @param locale The locale to use for the string representation.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* getFullDateString('2021-02-05', 'es')
|
|
190
|
+
* //=> 'viernes, 5 de febrero de 2021'
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* getFullDateString('2021-02-05', 'en')
|
|
196
|
+
* //=> 'Friday, February 5, 2021'
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
80
199
|
export const getFullDateString = (date, locale) => {
|
|
81
200
|
const sDateValue = sDate(date);
|
|
82
201
|
const utcDate = getDateAsUTCDateMini(sDateValue);
|
|
@@ -85,6 +204,34 @@ export const getFullDateString = (date, locale) => {
|
|
|
85
204
|
dateStyle: 'full',
|
|
86
205
|
});
|
|
87
206
|
};
|
|
207
|
+
/**
|
|
208
|
+
* Get the short string representation of the given date in the given locale.
|
|
209
|
+
*
|
|
210
|
+
* @param date The date to get the short string representation for. It can be an
|
|
211
|
+
* SDate or a string in the YYYY-MM-DD format.
|
|
212
|
+
* @param timeZone The time zone used to determine if in the current year. See
|
|
213
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
214
|
+
* @param locale The locale to use for the string representation.
|
|
215
|
+
* @param options The options to customize the short string representation.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* getShortDateString('2021-02-05', TestLocalTimeZone, 'en', {
|
|
220
|
+
* onTodayText,
|
|
221
|
+
* includeWeekday: false,
|
|
222
|
+
* }),
|
|
223
|
+
* //=> 'Feb 5' (year is not shown when in the current year)
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* getShortDateString('2021-02-05', TestLocalTimeZone, 'es', {
|
|
229
|
+
* onTodayText,
|
|
230
|
+
* includeWeekday: true,
|
|
231
|
+
* })
|
|
232
|
+
* //=> 'vie, 5 feb 21' (year when not in current year)
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
88
235
|
export const getShortDateString = (date, timeZone, locale, options) => {
|
|
89
236
|
const sDateValue = sDate(date);
|
|
90
237
|
if (isDateToday(sDateValue, timeZone)) {
|
|
@@ -96,71 +243,207 @@ export const getShortDateString = (date, timeZone, locale, options) => {
|
|
|
96
243
|
month: 'short',
|
|
97
244
|
day: 'numeric',
|
|
98
245
|
weekday: options.includeWeekday ? 'short' : undefined,
|
|
246
|
+
// Include year only if not current
|
|
99
247
|
year: isDateInCurrentYear(sDateValue, timeZone) ? undefined : '2-digit',
|
|
100
248
|
});
|
|
101
249
|
};
|
|
250
|
+
/**
|
|
251
|
+
* --- Operations ---
|
|
252
|
+
*/
|
|
253
|
+
/**
|
|
254
|
+
* Returns a new SDates instance with the date resulting from adding the given
|
|
255
|
+
* number of days to the given date. Because it adds calendar days rather than
|
|
256
|
+
* 24-hour days, this operation is not affected by time zones.
|
|
257
|
+
*
|
|
258
|
+
* @param date The date to add days to. It can be an SDate or a string in the
|
|
259
|
+
* YYYY-MM-DD format.
|
|
260
|
+
* @param days The number of days to add to the date.
|
|
261
|
+
*/
|
|
102
262
|
export const addDaysToDate = (date, days) => {
|
|
103
263
|
const sDateValue = sDate(date);
|
|
104
264
|
const nativeDate = getDateAsUTCDateMini(sDateValue);
|
|
105
265
|
nativeDate.setDate(nativeDate.getDate() + days);
|
|
106
266
|
return sDate(getISODateFromZonedDate(nativeDate));
|
|
107
267
|
};
|
|
268
|
+
/**
|
|
269
|
+
* Returns a new SDate instance with the date resulting from adding the given
|
|
270
|
+
* number of months to the given date. Because it just adds to the month
|
|
271
|
+
* component of the date, this operation is not affected by time zones.
|
|
272
|
+
*
|
|
273
|
+
* @param date The date to add months to. It can be an SDate or a string in the
|
|
274
|
+
* YYYY-MM-DD format.
|
|
275
|
+
* @param months The number of months to add to the date.
|
|
276
|
+
*/
|
|
108
277
|
export const addMonthsToDate = (date, months) => {
|
|
109
278
|
const sDateValue = sDate(date);
|
|
110
279
|
const nativeDate = getDateAsUTCDateMini(sDateValue);
|
|
111
280
|
nativeDate.setMonth(nativeDate.getMonth() + months);
|
|
112
281
|
return sDate(getISODateFromZonedDate(nativeDate));
|
|
113
282
|
};
|
|
283
|
+
/**
|
|
284
|
+
* Returns a new SDate instance with the date resulting from adding the given
|
|
285
|
+
* number of years to the given date. Because this only adds to the year
|
|
286
|
+
* component of the date, this method is not affected by leap years.
|
|
287
|
+
*
|
|
288
|
+
* @param date The date to add years to. It can be an SDate or a string in the
|
|
289
|
+
* YYYY-MM-DD format.
|
|
290
|
+
* @param years The number of years to add to the date.
|
|
291
|
+
*/
|
|
114
292
|
export const addYearsToDate = (date, years) => {
|
|
115
293
|
const sDateValue = sDate(date);
|
|
116
294
|
const nativeDate = getDateAsUTCDateMini(sDateValue);
|
|
117
295
|
nativeDate.setFullYear(nativeDate.getFullYear() + years);
|
|
118
296
|
return sDate(getISODateFromZonedDate(nativeDate));
|
|
119
297
|
};
|
|
298
|
+
/**
|
|
299
|
+
* --- Comparisons ---
|
|
300
|
+
*/
|
|
301
|
+
/**
|
|
302
|
+
* Returns true when the two given dates represent the same day and false
|
|
303
|
+
* otherwise.
|
|
304
|
+
*
|
|
305
|
+
* @param date1 The first date to compare. It can be an SDate or a string in the
|
|
306
|
+
* YYYY-MM-DD format.
|
|
307
|
+
* @param date2 The second date to compare. It can be an SDate or a string in
|
|
308
|
+
* the YYYY-MM-DD format.
|
|
309
|
+
*/
|
|
120
310
|
export const isSameDate = (date1, date2) => {
|
|
121
311
|
const sDate1 = sDate(date1);
|
|
122
312
|
const sDate2 = sDate(date2);
|
|
123
313
|
return sDate1.date === sDate2.date;
|
|
124
314
|
};
|
|
315
|
+
/**
|
|
316
|
+
* Returns true when the first date represents a date before the second date and
|
|
317
|
+
* false otherwise.
|
|
318
|
+
*
|
|
319
|
+
* @param date1 The first date to compare. It can be an SDate or a string in the
|
|
320
|
+
* YYYY-MM-DD format.
|
|
321
|
+
* @param date2 The second date to compare. It can be an SDate or a string in
|
|
322
|
+
* the YYYY-MM-DD format.
|
|
323
|
+
*/
|
|
125
324
|
export const isBeforeDate = (date1, date2) => {
|
|
126
325
|
const sDate1 = sDate(date1);
|
|
127
326
|
const sDate2 = sDate(date2);
|
|
128
327
|
return sDate1.date < sDate2.date;
|
|
129
328
|
};
|
|
329
|
+
/**
|
|
330
|
+
* Returns true when the first date represents a date that happens on the same
|
|
331
|
+
* date or before the second date and false otherwise.
|
|
332
|
+
*
|
|
333
|
+
* @param date1 The first date to compare. It can be an SDate or a string in the
|
|
334
|
+
* YYYY-MM-DD format.
|
|
335
|
+
* @param date2 The second date to compare. It can be an SDate or a string in the
|
|
336
|
+
* the YYYY-MM-DD format.
|
|
337
|
+
*/
|
|
130
338
|
export const isSameDateOrBefore = (date1, date2) => {
|
|
131
339
|
const sDate1 = sDate(date1);
|
|
132
340
|
const sDate2 = sDate(date2);
|
|
133
341
|
return sDate1.date <= sDate2.date;
|
|
134
342
|
};
|
|
343
|
+
/**
|
|
344
|
+
* Returns true when the first date represents a date that happens after the
|
|
345
|
+
* second date and false otherwise.
|
|
346
|
+
*
|
|
347
|
+
* @param date1 The first date to compare. It can be an SDate or a string in the
|
|
348
|
+
* YYYY-MM-DD format.
|
|
349
|
+
* @param date2 The second date to compare. It can be an SDate or a string in the
|
|
350
|
+
* the YYYY-MM-DD format.
|
|
351
|
+
*/
|
|
135
352
|
export const isAfterDate = (date1, date2) => {
|
|
136
353
|
const sDate1 = sDate(date1);
|
|
137
354
|
const sDate2 = sDate(date2);
|
|
138
355
|
return sDate1.date > sDate2.date;
|
|
139
356
|
};
|
|
357
|
+
/**
|
|
358
|
+
* Returns true when the first date represents a date that happens on the same
|
|
359
|
+
* date or after the second date and false otherwise.
|
|
360
|
+
*
|
|
361
|
+
* @param date1 The first date to compare. It can be an SDate or a string in the
|
|
362
|
+
* YYYY-MM-DD format.
|
|
363
|
+
* @param date2 The second date to compare. It can be an SDate or a string in the
|
|
364
|
+
* the YYYY-MM-DD format.
|
|
365
|
+
*/
|
|
140
366
|
export const isSameDateOrAfter = (date1, date2) => {
|
|
141
367
|
const sDate1 = sDate(date1);
|
|
142
368
|
const sDate2 = sDate(date2);
|
|
143
369
|
return sDate1.date >= sDate2.date;
|
|
144
370
|
};
|
|
371
|
+
/**
|
|
372
|
+
* Returns true when the date is today and false otherwise.
|
|
373
|
+
*
|
|
374
|
+
* @param date The date to check if it is today. It can be an SDate or a string
|
|
375
|
+
* in the YYYY-MM-DD format.
|
|
376
|
+
* @param timeZone The time zone to check if the date is today. See
|
|
377
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
378
|
+
*/
|
|
145
379
|
export const isDateToday = (date, timeZone) => {
|
|
146
380
|
const sDateValue = sDate(date);
|
|
147
381
|
return isSameDate(sDateValue, getDateToday(timeZone));
|
|
148
382
|
};
|
|
383
|
+
/**
|
|
384
|
+
* Returns true when the month on the first date is the same as the month on the
|
|
385
|
+
* second date. It also checks that the year is the same. Returns false
|
|
386
|
+
* otherwise.
|
|
387
|
+
*
|
|
388
|
+
* @param date1 The first date to compare. It can be an SDate or a string in the
|
|
389
|
+
* YYYY-MM-DD format.
|
|
390
|
+
* @param date2 The second date to compare. It can be an SDate or a string in
|
|
391
|
+
* the YYYY-MM-DD format.
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```ts
|
|
395
|
+
* areDatesInSameMonth('2021-02-05', '2021-02-15')
|
|
396
|
+
* //=> true
|
|
397
|
+
* ```
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* areDatesInSameMonth('2022-02-05', '2023-02-15')
|
|
402
|
+
* //=> false (different years)
|
|
403
|
+
* ```
|
|
404
|
+
*/
|
|
149
405
|
export const areDatesInSameMonth = (date1, date2) => {
|
|
150
406
|
const sDate1 = sDate(date1);
|
|
151
407
|
const sDate2 = sDate(date2);
|
|
152
408
|
return (getISOYearFromISODate(sDate1.date) === getISOYearFromISODate(sDate2.date) &&
|
|
153
409
|
getISOMonthFromISODate(sDate1.date) === getISOMonthFromISODate(sDate2.date));
|
|
154
410
|
};
|
|
411
|
+
/**
|
|
412
|
+
* Returns true when the date represents a date in the current month and year.
|
|
413
|
+
* Returns false otherwise.
|
|
414
|
+
*
|
|
415
|
+
* @param date The date to check if it is in the current month. It can be an
|
|
416
|
+
* SDate or a string in the YYYY-MM-DD format.
|
|
417
|
+
* @param timeZone The time zone to check if the date is in the current month.
|
|
418
|
+
* See `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
419
|
+
*/
|
|
155
420
|
export const isDateInCurrentMonth = (date, timeZone) => {
|
|
156
421
|
const sDateValue = sDate(date);
|
|
157
422
|
return areDatesInSameMonth(sDateValue, getDateToday(timeZone));
|
|
158
423
|
};
|
|
424
|
+
/**
|
|
425
|
+
* Returns true when the year of the first date is the same as the year on the
|
|
426
|
+
* second date. Returns false otherwise.
|
|
427
|
+
*
|
|
428
|
+
* @param date1 The first date to compare. It can be an SDate or a string in the
|
|
429
|
+
* YYYY-MM-DD format.
|
|
430
|
+
* @param date2 The second date to compare. It can be an SDate or a string in
|
|
431
|
+
* the YYYY-MM-DD format.
|
|
432
|
+
*/
|
|
159
433
|
export const areDatesInSameYear = (date1, date2) => {
|
|
160
434
|
const sDate1 = sDate(date1);
|
|
161
435
|
const sDate2 = sDate(date2);
|
|
162
436
|
return (getISOYearFromISODate(sDate1.date) === getISOYearFromISODate(sDate2.date));
|
|
163
437
|
};
|
|
438
|
+
/**
|
|
439
|
+
* Returns true when the year component of the date matches the current year in
|
|
440
|
+
* the given time zone. Returns false otherwise.
|
|
441
|
+
*
|
|
442
|
+
* @param date The date to check if it is in the current year. It can be an
|
|
443
|
+
* SDate or a string in the YYYY-MM-DD format.
|
|
444
|
+
* @param timeZone The time zone to check if the date is in the current year.
|
|
445
|
+
* See `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
446
|
+
*/
|
|
164
447
|
export const isDateInCurrentYear = (date, timeZone) => {
|
|
165
448
|
const sDateValue = sDate(date);
|
|
166
449
|
return areDatesInSameYear(sDateValue, getDateToday(timeZone));
|
package/dist/sTime.d.ts
CHANGED
|
@@ -1,18 +1,149 @@
|
|
|
1
1
|
import { STime } from './internal/STime.js';
|
|
2
|
+
/**
|
|
3
|
+
* --- Factory ---
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Returns a new STime instance.
|
|
7
|
+
*
|
|
8
|
+
* @param time And instance of STime or a string in the format 'HH:MM'.
|
|
9
|
+
*/
|
|
2
10
|
export declare const sTime: (time: string | STime) => STime;
|
|
11
|
+
/**
|
|
12
|
+
* --- Factory helpers ---
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Returns a new STime instance with the current time in the given time zone.
|
|
16
|
+
*
|
|
17
|
+
* @param timeZone The time zone to get the current time in. See
|
|
18
|
+
* `Intl.supportedValuesOf('timeZone')` for a list of valid time zones.
|
|
19
|
+
*/
|
|
3
20
|
export declare const getTimeNow: (timeZone: string) => STime;
|
|
21
|
+
/**
|
|
22
|
+
* Returns a new STime instance with the time at midnight (00:00).
|
|
23
|
+
*/
|
|
4
24
|
export declare const getTimeAtMidnight: () => STime;
|
|
25
|
+
/**
|
|
26
|
+
* Returns a new STime instance with the time resulting from adding the given
|
|
27
|
+
* number of minutes to midnight (00:00).
|
|
28
|
+
*
|
|
29
|
+
* @param timeInMinutes The number of minutes to add to midnight.
|
|
30
|
+
*/
|
|
5
31
|
export declare const getTimeFromMinutes: (timeInMinutes: number) => STime;
|
|
32
|
+
/**
|
|
33
|
+
* --- Getters ---
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* Returns the hours from the given time.
|
|
37
|
+
*
|
|
38
|
+
* @param time The time to get the hours from. It can be an STime or a string
|
|
39
|
+
* in the HH:MM format.
|
|
40
|
+
*/
|
|
6
41
|
export declare const getHoursFromTime: (time: string | STime) => number;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the hours from given time as a string (1 - 12).
|
|
44
|
+
*
|
|
45
|
+
* @param time The time to get the hours from. It can be an STime or a string
|
|
46
|
+
* in the HH:MM format.
|
|
47
|
+
*/
|
|
7
48
|
export declare const get12HoursHoursStringFromTime: (time: string | STime) => string;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the minutes from the given time.
|
|
51
|
+
*
|
|
52
|
+
* @param time The time to get the minutes from. It can be an STime or a string
|
|
53
|
+
* in the HH:MM format.
|
|
54
|
+
*/
|
|
8
55
|
export declare const getMinutesFromTime: (time: string | STime) => number;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the minutes from given time as a string (00-59).
|
|
58
|
+
*
|
|
59
|
+
* @param time The time to get the minutes from. It can be an STime or a string
|
|
60
|
+
* in the HH:MM format.
|
|
61
|
+
*/
|
|
9
62
|
export declare const getMinutesStringFromTime: (time: string | STime) => string;
|
|
63
|
+
/**
|
|
64
|
+
* Returns a string that represents the time in a 12-hour format (HH:MM AM/PM).
|
|
65
|
+
*
|
|
66
|
+
* @param time The time to get the string from. It can be an STime or a string
|
|
67
|
+
* in the HH:MM format.
|
|
68
|
+
*/
|
|
10
69
|
export declare const get12HourTimeString: (time: string | STime) => string;
|
|
70
|
+
/**
|
|
71
|
+
* Returns the time converted to minutes since midnight.
|
|
72
|
+
*
|
|
73
|
+
* @param time The time to get the minutes from. It can be an STime or a string
|
|
74
|
+
* in the HH:MM format.
|
|
75
|
+
*/
|
|
11
76
|
export declare const getTimeInMinutes: (time: string | STime, midnightIs24?: boolean) => number;
|
|
77
|
+
/**
|
|
78
|
+
* --- Operations ---
|
|
79
|
+
*/
|
|
80
|
+
/**
|
|
81
|
+
* Returns a new STime instance with the time resulting from adding the given
|
|
82
|
+
* number of minutes to the given time. The time will wrap around a 24 hour
|
|
83
|
+
* clock.
|
|
84
|
+
*
|
|
85
|
+
* @param time The time to add the minutes to. It can be an STime or a string
|
|
86
|
+
* in the HH:MM format.
|
|
87
|
+
* @param minutes The number of minutes to add.
|
|
88
|
+
*/
|
|
12
89
|
export declare const addMinutesToTime: (time: string | STime, minutes: number) => STime;
|
|
90
|
+
/**
|
|
91
|
+
* --- Comparisons ---
|
|
92
|
+
*/
|
|
93
|
+
/**
|
|
94
|
+
* Returns true when the two times are the same and false otherwise.
|
|
95
|
+
*
|
|
96
|
+
* @param time1 The first time to compare. It can be an STime or a string in the
|
|
97
|
+
* HH:MM format.
|
|
98
|
+
* @param time2 The second time to compare. It can be an STime or a string in
|
|
99
|
+
* the HH:MM format.
|
|
100
|
+
*/
|
|
13
101
|
export declare const isSameTime: (time1: string | STime, time2: string | STime) => boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Returns true when first time represents a time that happens after the second
|
|
104
|
+
* time. Returns false otherwise.
|
|
105
|
+
*
|
|
106
|
+
* @param time1 The first time to compare. It can be an STime or a string in the
|
|
107
|
+
* HH:MM format.
|
|
108
|
+
* @param time2 The second time to compare. It can be an STime or a string in
|
|
109
|
+
* the HH:MM format.
|
|
110
|
+
*/
|
|
14
111
|
export declare const isAfterTime: (time1: string | STime, time2: string | STime) => boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Returns true when first time represents a time that happens after or at the
|
|
114
|
+
* same time as the second time. Returns false otherwise.
|
|
115
|
+
*
|
|
116
|
+
* @param time1 The first time to compare. It can be an STime or a string in the
|
|
117
|
+
* HH:MM format.
|
|
118
|
+
* @param time2 The second time to compare. It can be an STime or a string in
|
|
119
|
+
* the HH:MM format.
|
|
120
|
+
*/
|
|
15
121
|
export declare const isSameTimeOrAfter: (time1: string | STime, time2: string | STime) => boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Returns true when first time represents a time that happens before the
|
|
124
|
+
* second time. Returns false otherwise.
|
|
125
|
+
*
|
|
126
|
+
* @param time1 The first time to compare. It can be an STime or a string in the
|
|
127
|
+
* HH:MM format.
|
|
128
|
+
* @param time2 The second time to compare. It can be an STime or a string in
|
|
129
|
+
* the HH:MM format.
|
|
130
|
+
*/
|
|
16
131
|
export declare const isBeforeTime: (time1: string | STime, time2: string | STime) => boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Returns true when first time represents a time that happens before or at the
|
|
134
|
+
* same time as the second time. Returns false otherwise.
|
|
135
|
+
*
|
|
136
|
+
* @param time1 The first time to compare. It can be an STime or a string in the
|
|
137
|
+
* HH:MM format.
|
|
138
|
+
* @param time2 The second time to compare. It can be an STime or a string in
|
|
139
|
+
* the HH:MM format.
|
|
140
|
+
*/
|
|
17
141
|
export declare const isSameTimeOrBefore: (time1: string | STime, time2: string | STime) => boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Returns true when the given time is at or after noon (12:00) and false
|
|
144
|
+
* otherwise.
|
|
145
|
+
*
|
|
146
|
+
* @param time The time to check. It can be an STime or a string in the HH:MM
|
|
147
|
+
* format.
|
|
148
|
+
*/
|
|
18
149
|
export declare const isTimePM: (time: string | STime) => boolean;
|