ts-time-utils 1.1.0 → 2.0.0
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/README.md +458 -12
- package/dist/calculate.d.ts +7 -2
- package/dist/calculate.d.ts.map +1 -1
- package/dist/calculate.js +13 -3
- package/dist/calendar.d.ts +103 -0
- package/dist/calendar.d.ts.map +1 -1
- package/dist/calendar.js +224 -0
- package/dist/compare.d.ts +217 -0
- package/dist/compare.d.ts.map +1 -0
- package/dist/compare.js +417 -0
- package/dist/cron.d.ts +82 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +294 -0
- package/dist/esm/calculate.d.ts +7 -2
- package/dist/esm/calculate.d.ts.map +1 -1
- package/dist/esm/calculate.js +13 -3
- package/dist/esm/calendar.d.ts +103 -0
- package/dist/esm/calendar.d.ts.map +1 -1
- package/dist/esm/calendar.js +224 -0
- package/dist/esm/compare.d.ts +217 -0
- package/dist/esm/compare.d.ts.map +1 -0
- package/dist/esm/compare.js +417 -0
- package/dist/esm/cron.d.ts +82 -0
- package/dist/esm/cron.d.ts.map +1 -0
- package/dist/esm/cron.js +294 -0
- package/dist/esm/fiscal.d.ts +195 -0
- package/dist/esm/fiscal.d.ts.map +1 -0
- package/dist/esm/fiscal.js +295 -0
- package/dist/esm/format.d.ts +65 -0
- package/dist/esm/format.d.ts.map +1 -1
- package/dist/esm/format.js +202 -0
- package/dist/esm/index.d.ts +13 -6
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +14 -6
- package/dist/esm/iterate.d.ts +212 -0
- package/dist/esm/iterate.d.ts.map +1 -0
- package/dist/esm/iterate.js +409 -0
- package/dist/esm/parse.d.ts +45 -0
- package/dist/esm/parse.d.ts.map +1 -1
- package/dist/esm/parse.js +207 -0
- package/dist/esm/timezone.d.ts +52 -0
- package/dist/esm/timezone.d.ts.map +1 -1
- package/dist/esm/timezone.js +171 -0
- package/dist/esm/validate.d.ts +51 -0
- package/dist/esm/validate.d.ts.map +1 -1
- package/dist/esm/validate.js +92 -0
- package/dist/esm/workingHours.d.ts +70 -0
- package/dist/esm/workingHours.d.ts.map +1 -1
- package/dist/esm/workingHours.js +161 -0
- package/dist/fiscal.d.ts +195 -0
- package/dist/fiscal.d.ts.map +1 -0
- package/dist/fiscal.js +295 -0
- package/dist/format.d.ts +65 -0
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +202 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -6
- package/dist/iterate.d.ts +212 -0
- package/dist/iterate.d.ts.map +1 -0
- package/dist/iterate.js +409 -0
- package/dist/parse.d.ts +45 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +207 -0
- package/dist/timezone.d.ts +52 -0
- package/dist/timezone.d.ts.map +1 -1
- package/dist/timezone.js +171 -0
- package/dist/validate.d.ts +51 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +92 -0
- package/dist/workingHours.d.ts +70 -0
- package/dist/workingHours.d.ts.map +1 -1
- package/dist/workingHours.js +161 -0
- package/package.json +30 -11
package/dist/esm/parse.d.ts
CHANGED
|
@@ -28,4 +28,49 @@ export declare function parseCustomFormat(dateString: string, format: string): D
|
|
|
28
28
|
* @param formats - array of format patterns to try
|
|
29
29
|
*/
|
|
30
30
|
export declare function parseManyFormats(dateString: string, formats: string[]): Date | null;
|
|
31
|
+
/**
|
|
32
|
+
* Parse an ISO 8601 duration string (e.g., "P1Y2M3DT4H5M6S")
|
|
33
|
+
* @param duration - ISO 8601 duration string
|
|
34
|
+
* @returns object with parsed components
|
|
35
|
+
*/
|
|
36
|
+
export declare function parseISO8601Duration(duration: string): {
|
|
37
|
+
years: number;
|
|
38
|
+
months: number;
|
|
39
|
+
weeks: number;
|
|
40
|
+
days: number;
|
|
41
|
+
hours: number;
|
|
42
|
+
minutes: number;
|
|
43
|
+
seconds: number;
|
|
44
|
+
} | null;
|
|
45
|
+
/**
|
|
46
|
+
* Convert ISO 8601 duration to milliseconds (approximate for months/years)
|
|
47
|
+
* @param duration - ISO 8601 duration string
|
|
48
|
+
*/
|
|
49
|
+
export declare function parseISO8601DurationToMs(duration: string): number | null;
|
|
50
|
+
/**
|
|
51
|
+
* Parse a time string (e.g., "14:30", "2:30 PM", "14:30:45")
|
|
52
|
+
* @param timeString - time string to parse
|
|
53
|
+
* @returns object with hours, minutes, seconds, or null if invalid
|
|
54
|
+
*/
|
|
55
|
+
export declare function parseTime(timeString: string): {
|
|
56
|
+
hours: number;
|
|
57
|
+
minutes: number;
|
|
58
|
+
seconds: number;
|
|
59
|
+
} | null;
|
|
60
|
+
/**
|
|
61
|
+
* Guess the date format of a date string
|
|
62
|
+
* @param dateString - date string to analyze
|
|
63
|
+
* @returns detected format pattern or null
|
|
64
|
+
*/
|
|
65
|
+
export declare function guessDateFormat(dateString: string): string | null;
|
|
66
|
+
/**
|
|
67
|
+
* Parse a date string using auto-detected format
|
|
68
|
+
* @param dateString - date string to parse
|
|
69
|
+
*/
|
|
70
|
+
export declare function parseAutoFormat(dateString: string): Date | null;
|
|
71
|
+
/**
|
|
72
|
+
* Parse a date from a natural language date range endpoint
|
|
73
|
+
* @param input - string like "end of month", "start of year", "beginning of week"
|
|
74
|
+
*/
|
|
75
|
+
export declare function parseRangeEndpoint(input: string): Date | null;
|
|
31
76
|
//# sourceMappingURL=parse.d.ts.map
|
package/dist/esm/parse.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../src/parse.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAuDpE;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CA6C5D;AAED;;;GAGG;AACH,wB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AED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAqBxE;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,UAAU,EAAE,MAAM,GAAG;IAC7C,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CACjB,GAAG,IAAI,CAuCP;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAoCjE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAmB/D;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CA2C7D"}
|
package/dist/esm/parse.js
CHANGED
|
@@ -215,3 +215,210 @@ function addTimeUnits(date, amount, unit) {
|
|
|
215
215
|
function subtractTimeUnits(date, amount, unit) {
|
|
216
216
|
return addTimeUnits(date, -amount, unit);
|
|
217
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Parse an ISO 8601 duration string (e.g., "P1Y2M3DT4H5M6S")
|
|
220
|
+
* @param duration - ISO 8601 duration string
|
|
221
|
+
* @returns object with parsed components
|
|
222
|
+
*/
|
|
223
|
+
export function parseISO8601Duration(duration) {
|
|
224
|
+
// ISO 8601 duration format: P[n]Y[n]M[n]DT[n]H[n]M[n]S or P[n]W
|
|
225
|
+
const result = {
|
|
226
|
+
years: 0,
|
|
227
|
+
months: 0,
|
|
228
|
+
weeks: 0,
|
|
229
|
+
days: 0,
|
|
230
|
+
hours: 0,
|
|
231
|
+
minutes: 0,
|
|
232
|
+
seconds: 0,
|
|
233
|
+
};
|
|
234
|
+
const normalized = duration.toUpperCase().trim();
|
|
235
|
+
if (!normalized.startsWith('P')) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
// Handle week format: P[n]W
|
|
239
|
+
const weekMatch = normalized.match(/^P(\d+)W$/);
|
|
240
|
+
if (weekMatch) {
|
|
241
|
+
result.weeks = parseInt(weekMatch[1]);
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
// Full format: P[n]Y[n]M[n]DT[n]H[n]M[n]S
|
|
245
|
+
const fullMatch = normalized.match(/^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/);
|
|
246
|
+
if (!fullMatch) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
const [, years, months, days, hours, minutes, seconds] = fullMatch;
|
|
250
|
+
if (years)
|
|
251
|
+
result.years = parseInt(years);
|
|
252
|
+
if (months)
|
|
253
|
+
result.months = parseInt(months);
|
|
254
|
+
if (days)
|
|
255
|
+
result.days = parseInt(days);
|
|
256
|
+
if (hours)
|
|
257
|
+
result.hours = parseInt(hours);
|
|
258
|
+
if (minutes)
|
|
259
|
+
result.minutes = parseInt(minutes);
|
|
260
|
+
if (seconds)
|
|
261
|
+
result.seconds = parseFloat(seconds);
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Convert ISO 8601 duration to milliseconds (approximate for months/years)
|
|
266
|
+
* @param duration - ISO 8601 duration string
|
|
267
|
+
*/
|
|
268
|
+
export function parseISO8601DurationToMs(duration) {
|
|
269
|
+
const parsed = parseISO8601Duration(duration);
|
|
270
|
+
if (!parsed)
|
|
271
|
+
return null;
|
|
272
|
+
const MS_PER_SECOND = 1000;
|
|
273
|
+
const MS_PER_MINUTE = 60 * MS_PER_SECOND;
|
|
274
|
+
const MS_PER_HOUR = 60 * MS_PER_MINUTE;
|
|
275
|
+
const MS_PER_DAY = 24 * MS_PER_HOUR;
|
|
276
|
+
const MS_PER_WEEK = 7 * MS_PER_DAY;
|
|
277
|
+
const MS_PER_MONTH = 30 * MS_PER_DAY; // Approximate
|
|
278
|
+
const MS_PER_YEAR = 365 * MS_PER_DAY; // Approximate
|
|
279
|
+
return (parsed.years * MS_PER_YEAR +
|
|
280
|
+
parsed.months * MS_PER_MONTH +
|
|
281
|
+
parsed.weeks * MS_PER_WEEK +
|
|
282
|
+
parsed.days * MS_PER_DAY +
|
|
283
|
+
parsed.hours * MS_PER_HOUR +
|
|
284
|
+
parsed.minutes * MS_PER_MINUTE +
|
|
285
|
+
parsed.seconds * MS_PER_SECOND);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Parse a time string (e.g., "14:30", "2:30 PM", "14:30:45")
|
|
289
|
+
* @param timeString - time string to parse
|
|
290
|
+
* @returns object with hours, minutes, seconds, or null if invalid
|
|
291
|
+
*/
|
|
292
|
+
export function parseTime(timeString) {
|
|
293
|
+
const normalized = timeString.trim();
|
|
294
|
+
// 24-hour format: HH:MM or HH:MM:SS
|
|
295
|
+
const match24 = normalized.match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?$/);
|
|
296
|
+
if (match24) {
|
|
297
|
+
const hours = parseInt(match24[1]);
|
|
298
|
+
const minutes = parseInt(match24[2]);
|
|
299
|
+
const seconds = match24[3] ? parseInt(match24[3]) : 0;
|
|
300
|
+
if (hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59 && seconds >= 0 && seconds <= 59) {
|
|
301
|
+
return { hours, minutes, seconds };
|
|
302
|
+
}
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
// 12-hour format: H:MM AM/PM or HH:MM AM/PM
|
|
306
|
+
const match12 = normalized.match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?\s*(AM|PM|am|pm)$/i);
|
|
307
|
+
if (match12) {
|
|
308
|
+
let hours = parseInt(match12[1]);
|
|
309
|
+
const minutes = parseInt(match12[2]);
|
|
310
|
+
const seconds = match12[3] ? parseInt(match12[3]) : 0;
|
|
311
|
+
const isPM = match12[4].toLowerCase() === 'pm';
|
|
312
|
+
if (hours < 1 || hours > 12 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59) {
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
// Convert to 24-hour format
|
|
316
|
+
if (isPM && hours !== 12) {
|
|
317
|
+
hours += 12;
|
|
318
|
+
}
|
|
319
|
+
else if (!isPM && hours === 12) {
|
|
320
|
+
hours = 0;
|
|
321
|
+
}
|
|
322
|
+
return { hours, minutes, seconds };
|
|
323
|
+
}
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Guess the date format of a date string
|
|
328
|
+
* @param dateString - date string to analyze
|
|
329
|
+
* @returns detected format pattern or null
|
|
330
|
+
*/
|
|
331
|
+
export function guessDateFormat(dateString) {
|
|
332
|
+
const normalized = dateString.trim();
|
|
333
|
+
// YYYY-MM-DD
|
|
334
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(normalized)) {
|
|
335
|
+
return 'YYYY-MM-DD';
|
|
336
|
+
}
|
|
337
|
+
// DD/MM/YYYY or MM/DD/YYYY - need to analyze values
|
|
338
|
+
if (/^\d{2}\/\d{2}\/\d{4}$/.test(normalized)) {
|
|
339
|
+
const [first, second] = normalized.split('/').map(Number);
|
|
340
|
+
if (first > 12)
|
|
341
|
+
return 'DD/MM/YYYY'; // First must be day
|
|
342
|
+
if (second > 12)
|
|
343
|
+
return 'MM/DD/YYYY'; // Second must be day
|
|
344
|
+
// Ambiguous - default to US format
|
|
345
|
+
return 'MM/DD/YYYY';
|
|
346
|
+
}
|
|
347
|
+
// DD-MM-YYYY or MM-DD-YYYY
|
|
348
|
+
if (/^\d{2}-\d{2}-\d{4}$/.test(normalized)) {
|
|
349
|
+
const [first, second] = normalized.split('-').map(Number);
|
|
350
|
+
if (first > 12)
|
|
351
|
+
return 'DD-MM-YYYY';
|
|
352
|
+
if (second > 12)
|
|
353
|
+
return 'MM-DD-YYYY';
|
|
354
|
+
return 'MM-DD-YYYY';
|
|
355
|
+
}
|
|
356
|
+
// YYYYMMDD
|
|
357
|
+
if (/^\d{8}$/.test(normalized)) {
|
|
358
|
+
return 'YYYYMMDD';
|
|
359
|
+
}
|
|
360
|
+
// ISO 8601 with time
|
|
361
|
+
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(normalized)) {
|
|
362
|
+
return 'ISO8601';
|
|
363
|
+
}
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Parse a date string using auto-detected format
|
|
368
|
+
* @param dateString - date string to parse
|
|
369
|
+
*/
|
|
370
|
+
export function parseAutoFormat(dateString) {
|
|
371
|
+
const format = guessDateFormat(dateString);
|
|
372
|
+
if (!format) {
|
|
373
|
+
return parseDate(dateString);
|
|
374
|
+
}
|
|
375
|
+
if (format === 'ISO8601') {
|
|
376
|
+
return parseDate(dateString);
|
|
377
|
+
}
|
|
378
|
+
if (format === 'YYYYMMDD') {
|
|
379
|
+
const year = parseInt(dateString.slice(0, 4));
|
|
380
|
+
const month = parseInt(dateString.slice(4, 6)) - 1;
|
|
381
|
+
const day = parseInt(dateString.slice(6, 8));
|
|
382
|
+
return new Date(year, month, day);
|
|
383
|
+
}
|
|
384
|
+
return parseCustomFormat(dateString, format);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Parse a date from a natural language date range endpoint
|
|
388
|
+
* @param input - string like "end of month", "start of year", "beginning of week"
|
|
389
|
+
*/
|
|
390
|
+
export function parseRangeEndpoint(input) {
|
|
391
|
+
const now = new Date();
|
|
392
|
+
const lowercaseInput = input.toLowerCase().trim();
|
|
393
|
+
// Start/beginning of period
|
|
394
|
+
if (lowercaseInput.match(/^(start|beginning)\s+of\s+(this\s+)?(day|today)$/)) {
|
|
395
|
+
return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
|
|
396
|
+
}
|
|
397
|
+
if (lowercaseInput.match(/^(start|beginning)\s+of\s+(this\s+)?week$/)) {
|
|
398
|
+
const day = now.getDay();
|
|
399
|
+
const diff = now.getDate() - day + (day === 0 ? -6 : 1); // Monday
|
|
400
|
+
return new Date(now.getFullYear(), now.getMonth(), diff, 0, 0, 0, 0);
|
|
401
|
+
}
|
|
402
|
+
if (lowercaseInput.match(/^(start|beginning)\s+of\s+(this\s+)?month$/)) {
|
|
403
|
+
return new Date(now.getFullYear(), now.getMonth(), 1, 0, 0, 0, 0);
|
|
404
|
+
}
|
|
405
|
+
if (lowercaseInput.match(/^(start|beginning)\s+of\s+(this\s+)?year$/)) {
|
|
406
|
+
return new Date(now.getFullYear(), 0, 1, 0, 0, 0, 0);
|
|
407
|
+
}
|
|
408
|
+
// End of period
|
|
409
|
+
if (lowercaseInput.match(/^end\s+of\s+(this\s+)?(day|today)$/)) {
|
|
410
|
+
return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999);
|
|
411
|
+
}
|
|
412
|
+
if (lowercaseInput.match(/^end\s+of\s+(this\s+)?week$/)) {
|
|
413
|
+
const day = now.getDay();
|
|
414
|
+
const diff = now.getDate() - day + (day === 0 ? 0 : 7); // Sunday
|
|
415
|
+
return new Date(now.getFullYear(), now.getMonth(), diff, 23, 59, 59, 999);
|
|
416
|
+
}
|
|
417
|
+
if (lowercaseInput.match(/^end\s+of\s+(this\s+)?month$/)) {
|
|
418
|
+
return new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999);
|
|
419
|
+
}
|
|
420
|
+
if (lowercaseInput.match(/^end\s+of\s+(this\s+)?year$/)) {
|
|
421
|
+
return new Date(now.getFullYear(), 11, 31, 23, 59, 59, 999);
|
|
422
|
+
}
|
|
423
|
+
return null;
|
|
424
|
+
}
|
package/dist/esm/timezone.d.ts
CHANGED
|
@@ -31,4 +31,56 @@ export declare function compareZoneOffsets(zoneA: string, zoneB: string, date?:
|
|
|
31
31
|
* For example useful for naive scheduling.
|
|
32
32
|
*/
|
|
33
33
|
export declare function reinterpretAsZone(date: Date, targetZone: string): Date | null;
|
|
34
|
+
/**
|
|
35
|
+
* Check if a date is in Daylight Saving Time for a given timezone
|
|
36
|
+
* @param date - date to check
|
|
37
|
+
* @param zone - IANA timezone string
|
|
38
|
+
*/
|
|
39
|
+
export declare function isDST(date: Date, zone: string): boolean | null;
|
|
40
|
+
/**
|
|
41
|
+
* Get the next DST transition (if any) for a timezone
|
|
42
|
+
* @param date - starting date
|
|
43
|
+
* @param zone - IANA timezone string
|
|
44
|
+
* @returns next DST transition date or null if no DST in that zone
|
|
45
|
+
*/
|
|
46
|
+
export declare function getNextDSTTransition(date: Date, zone: string): Date | null;
|
|
47
|
+
/**
|
|
48
|
+
* Find overlapping working hours between multiple timezones
|
|
49
|
+
* @param zones - array of IANA timezone strings
|
|
50
|
+
* @param workHoursStart - work hours start (0-24)
|
|
51
|
+
* @param workHoursEnd - work hours end (0-24)
|
|
52
|
+
* @param date - reference date (default: today)
|
|
53
|
+
* @returns array of overlapping hour ranges in UTC, or null if no overlap
|
|
54
|
+
*/
|
|
55
|
+
export declare function findCommonWorkingHours(zones: string[], workHoursStart?: number, workHoursEnd?: number, date?: Date): {
|
|
56
|
+
startUTC: number;
|
|
57
|
+
endUTC: number;
|
|
58
|
+
} | null;
|
|
59
|
+
/**
|
|
60
|
+
* Get all timezone abbreviations for a zone on a given date
|
|
61
|
+
* @param zone - IANA timezone string
|
|
62
|
+
* @param date - reference date
|
|
63
|
+
*/
|
|
64
|
+
export declare function getTimezoneAbbreviation(zone: string, date?: Date): string | null;
|
|
65
|
+
/**
|
|
66
|
+
* Convert a wall clock time from one timezone to another
|
|
67
|
+
* @param date - date with time in source timezone
|
|
68
|
+
* @param fromZone - source timezone
|
|
69
|
+
* @param toZone - target timezone
|
|
70
|
+
*/
|
|
71
|
+
export declare function convertBetweenZones(date: Date, fromZone: string, toZone: string): Date | null;
|
|
72
|
+
/**
|
|
73
|
+
* Get the time difference between two timezones in hours
|
|
74
|
+
* @param zoneA - first timezone
|
|
75
|
+
* @param zoneB - second timezone
|
|
76
|
+
* @param date - reference date
|
|
77
|
+
*/
|
|
78
|
+
export declare function getTimezoneDifferenceHours(zoneA: string, zoneB: string, date?: Date): number | null;
|
|
79
|
+
/**
|
|
80
|
+
* Check if two timezones have the same offset at a given date
|
|
81
|
+
* @param zoneA - first timezone
|
|
82
|
+
* @param zoneB - second timezone
|
|
83
|
+
* @param date - reference date
|
|
84
|
+
*/
|
|
85
|
+
export declare function isSameTimezone(zoneA: string, zoneB: string, date?: Date): boolean | null;
|
|
34
86
|
//# sourceMappingURL=timezone.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timezone.d.ts","sourceRoot":"","sources":["../../src/timezone.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,sDAAsD;AACtD,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,IAAiB,GAAG,MAAM,GAAG,IAAI,CAgBtF;AAED,iCAAiC;AACjC,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,IAAI,CAAC,qBAA0B,GAAG,MAAM,CAG3G;AAED,yCAAyC;AACzC,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAIvE;AAED,qFAAqF;AACrF,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;CAAE,GAAG,IAAI,CAoB9J;AAED,yDAAyD;AACzD,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,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,IAAI,GAAE,IAAiB,GAAG,MAAM,GAAG,IAAI,CAI/G;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,IAAiB,GAAG,OAAO,GAAG,IAAI,CAIpG"}
|
package/dist/esm/timezone.js
CHANGED
|
@@ -97,3 +97,174 @@ export function reinterpretAsZone(date, targetZone) {
|
|
|
97
97
|
return null;
|
|
98
98
|
return new Date(Date.UTC(target.year, target.month - 1, target.day, target.hour, target.minute, target.second));
|
|
99
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Check if a date is in Daylight Saving Time for a given timezone
|
|
102
|
+
* @param date - date to check
|
|
103
|
+
* @param zone - IANA timezone string
|
|
104
|
+
*/
|
|
105
|
+
export function isDST(date, zone) {
|
|
106
|
+
if (!isValidTimeZone(zone))
|
|
107
|
+
return null;
|
|
108
|
+
// Compare offset in January vs July - the one with larger offset is DST
|
|
109
|
+
const january = new Date(date.getFullYear(), 0, 1);
|
|
110
|
+
const july = new Date(date.getFullYear(), 6, 1);
|
|
111
|
+
const janOffset = getTimezoneOffset(zone, january);
|
|
112
|
+
const julOffset = getTimezoneOffset(zone, july);
|
|
113
|
+
const currentOffset = getTimezoneOffset(zone, date);
|
|
114
|
+
if (janOffset === null || julOffset === null || currentOffset === null) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
// If offsets are the same, no DST in this zone
|
|
118
|
+
if (janOffset === julOffset) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
// In northern hemisphere, summer (July) has larger offset
|
|
122
|
+
// In southern hemisphere, summer (January) has larger offset
|
|
123
|
+
// DST is whichever is the "larger" offset
|
|
124
|
+
const maxOffset = Math.max(janOffset, julOffset);
|
|
125
|
+
return currentOffset === maxOffset;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get the next DST transition (if any) for a timezone
|
|
129
|
+
* @param date - starting date
|
|
130
|
+
* @param zone - IANA timezone string
|
|
131
|
+
* @returns next DST transition date or null if no DST in that zone
|
|
132
|
+
*/
|
|
133
|
+
export function getNextDSTTransition(date, zone) {
|
|
134
|
+
if (!isValidTimeZone(zone))
|
|
135
|
+
return null;
|
|
136
|
+
const january = new Date(date.getFullYear(), 0, 1);
|
|
137
|
+
const july = new Date(date.getFullYear(), 6, 1);
|
|
138
|
+
const janOffset = getTimezoneOffset(zone, january);
|
|
139
|
+
const julOffset = getTimezoneOffset(zone, july);
|
|
140
|
+
if (janOffset === null || julOffset === null)
|
|
141
|
+
return null;
|
|
142
|
+
// No DST if offsets are the same
|
|
143
|
+
if (janOffset === julOffset)
|
|
144
|
+
return null;
|
|
145
|
+
// Binary search for the transition within the next year
|
|
146
|
+
const currentOffset = getTimezoneOffset(zone, date);
|
|
147
|
+
if (currentOffset === null)
|
|
148
|
+
return null;
|
|
149
|
+
// Check day by day for up to 366 days
|
|
150
|
+
const searchDate = new Date(date);
|
|
151
|
+
for (let i = 1; i <= 366; i++) {
|
|
152
|
+
searchDate.setDate(searchDate.getDate() + 1);
|
|
153
|
+
const newOffset = getTimezoneOffset(zone, searchDate);
|
|
154
|
+
if (newOffset !== null && newOffset !== currentOffset) {
|
|
155
|
+
// Found a transition, now narrow it down
|
|
156
|
+
const prevDay = new Date(searchDate);
|
|
157
|
+
prevDay.setDate(prevDay.getDate() - 1);
|
|
158
|
+
// Binary search within the day
|
|
159
|
+
let low = prevDay.getTime();
|
|
160
|
+
let high = searchDate.getTime();
|
|
161
|
+
while (high - low > 60000) { // 1 minute precision
|
|
162
|
+
const mid = Math.floor((low + high) / 2);
|
|
163
|
+
const midDate = new Date(mid);
|
|
164
|
+
const midOffset = getTimezoneOffset(zone, midDate);
|
|
165
|
+
if (midOffset === currentOffset) {
|
|
166
|
+
low = mid;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
high = mid;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return new Date(high);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Find overlapping working hours between multiple timezones
|
|
179
|
+
* @param zones - array of IANA timezone strings
|
|
180
|
+
* @param workHoursStart - work hours start (0-24)
|
|
181
|
+
* @param workHoursEnd - work hours end (0-24)
|
|
182
|
+
* @param date - reference date (default: today)
|
|
183
|
+
* @returns array of overlapping hour ranges in UTC, or null if no overlap
|
|
184
|
+
*/
|
|
185
|
+
export function findCommonWorkingHours(zones, workHoursStart = 9, workHoursEnd = 17, date = new Date()) {
|
|
186
|
+
if (zones.length === 0)
|
|
187
|
+
return null;
|
|
188
|
+
// Convert each zone's work hours to UTC
|
|
189
|
+
const utcRanges = zones.map(zone => {
|
|
190
|
+
const offset = getTimezoneOffset(zone, date);
|
|
191
|
+
if (offset === null)
|
|
192
|
+
return null;
|
|
193
|
+
// Offset is in minutes, positive means ahead of UTC
|
|
194
|
+
// So to convert local time to UTC, we subtract the offset
|
|
195
|
+
const startUTC = workHoursStart - (offset / 60);
|
|
196
|
+
const endUTC = workHoursEnd - (offset / 60);
|
|
197
|
+
return { startUTC, endUTC };
|
|
198
|
+
});
|
|
199
|
+
if (utcRanges.some(r => r === null))
|
|
200
|
+
return null;
|
|
201
|
+
const validRanges = utcRanges;
|
|
202
|
+
// Find intersection of all ranges
|
|
203
|
+
let overlapStart = Math.max(...validRanges.map(r => r.startUTC));
|
|
204
|
+
let overlapEnd = Math.min(...validRanges.map(r => r.endUTC));
|
|
205
|
+
if (overlapStart >= overlapEnd) {
|
|
206
|
+
return null; // No overlap
|
|
207
|
+
}
|
|
208
|
+
return { startUTC: overlapStart, endUTC: overlapEnd };
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Get all timezone abbreviations for a zone on a given date
|
|
212
|
+
* @param zone - IANA timezone string
|
|
213
|
+
* @param date - reference date
|
|
214
|
+
*/
|
|
215
|
+
export function getTimezoneAbbreviation(zone, date = new Date()) {
|
|
216
|
+
try {
|
|
217
|
+
const fmt = new Intl.DateTimeFormat('en-US', {
|
|
218
|
+
timeZone: zone,
|
|
219
|
+
timeZoneName: 'short'
|
|
220
|
+
});
|
|
221
|
+
const parts = fmt.formatToParts(date);
|
|
222
|
+
const tzPart = parts.find(p => p.type === 'timeZoneName');
|
|
223
|
+
return tzPart?.value || null;
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Convert a wall clock time from one timezone to another
|
|
231
|
+
* @param date - date with time in source timezone
|
|
232
|
+
* @param fromZone - source timezone
|
|
233
|
+
* @param toZone - target timezone
|
|
234
|
+
*/
|
|
235
|
+
export function convertBetweenZones(date, fromZone, toZone) {
|
|
236
|
+
// First, interpret the date as being in fromZone
|
|
237
|
+
const fromOffset = getTimezoneOffset(fromZone, date);
|
|
238
|
+
const toOffset = getTimezoneOffset(toZone, date);
|
|
239
|
+
if (fromOffset === null || toOffset === null)
|
|
240
|
+
return null;
|
|
241
|
+
// Calculate the difference in minutes
|
|
242
|
+
const diffMinutes = toOffset - fromOffset;
|
|
243
|
+
// Apply the difference
|
|
244
|
+
const result = new Date(date.getTime() + diffMinutes * 60 * 1000);
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Get the time difference between two timezones in hours
|
|
249
|
+
* @param zoneA - first timezone
|
|
250
|
+
* @param zoneB - second timezone
|
|
251
|
+
* @param date - reference date
|
|
252
|
+
*/
|
|
253
|
+
export function getTimezoneDifferenceHours(zoneA, zoneB, date = new Date()) {
|
|
254
|
+
const diff = compareZoneOffsets(zoneA, zoneB, date);
|
|
255
|
+
if (diff === null)
|
|
256
|
+
return null;
|
|
257
|
+
return diff / 60;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Check if two timezones have the same offset at a given date
|
|
261
|
+
* @param zoneA - first timezone
|
|
262
|
+
* @param zoneB - second timezone
|
|
263
|
+
* @param date - reference date
|
|
264
|
+
*/
|
|
265
|
+
export function isSameTimezone(zoneA, zoneB, date = new Date()) {
|
|
266
|
+
const diff = compareZoneOffsets(zoneA, zoneB, date);
|
|
267
|
+
if (diff === null)
|
|
268
|
+
return null;
|
|
269
|
+
return diff === 0;
|
|
270
|
+
}
|
package/dist/esm/validate.d.ts
CHANGED
|
@@ -59,4 +59,55 @@ export declare function isValidTimeString(time: string): boolean;
|
|
|
59
59
|
* @param dateString - date string to validate
|
|
60
60
|
*/
|
|
61
61
|
export declare function isValidISOString(dateString: string): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Check if two dates are in the same week (ISO week, Monday-Sunday)
|
|
64
|
+
* @param date1 - first date
|
|
65
|
+
* @param date2 - second date
|
|
66
|
+
*/
|
|
67
|
+
export declare function isSameWeek(date1: Date, date2: Date): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Check if two dates are in the same month
|
|
70
|
+
* @param date1 - first date
|
|
71
|
+
* @param date2 - second date
|
|
72
|
+
*/
|
|
73
|
+
export declare function isSameMonth(date1: Date, date2: Date): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Check if two dates are in the same year
|
|
76
|
+
* @param date1 - first date
|
|
77
|
+
* @param date2 - second date
|
|
78
|
+
*/
|
|
79
|
+
export declare function isSameYear(date1: Date, date2: Date): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Check if a date is in the current week
|
|
82
|
+
* @param date - date to check
|
|
83
|
+
*/
|
|
84
|
+
export declare function isThisWeek(date: Date): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Check if a date is in the current month
|
|
87
|
+
* @param date - date to check
|
|
88
|
+
*/
|
|
89
|
+
export declare function isThisMonth(date: Date): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Check if a date is in the current year
|
|
92
|
+
* @param date - date to check
|
|
93
|
+
*/
|
|
94
|
+
export declare function isThisYear(date: Date): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Check if a date is a business day (weekday, optionally excluding holidays)
|
|
97
|
+
* @param date - date to check
|
|
98
|
+
* @param holidays - optional array of holiday dates to exclude
|
|
99
|
+
*/
|
|
100
|
+
export declare function isBusinessDay(date: Date, holidays?: Date[]): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Check if a date is in the last N days (including today)
|
|
103
|
+
* @param date - date to check
|
|
104
|
+
* @param n - number of days
|
|
105
|
+
*/
|
|
106
|
+
export declare function isInLastNDays(date: Date, n: number): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Check if a date is in the next N days (including today)
|
|
109
|
+
* @param date - date to check
|
|
110
|
+
* @param n - number of days
|
|
111
|
+
*/
|
|
112
|
+
export declare function isInNextNDays(date: Date, n: number): boolean;
|
|
62
113
|
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../src/validate.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAWjE;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEhD;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE1C;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE5C;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAO3C;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAQ/C;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAQ9C;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,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AGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAGvD;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAG5D;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,CAW5D;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,CAK7D;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,CAE5D;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE9C;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE/C;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE9C;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,GAAE,IAAI,EAAO,GAAG,OAAO,CAIxE;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAU5D;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAU5D"}
|
package/dist/esm/validate.js
CHANGED
|
@@ -106,3 +106,95 @@ export function isValidISOString(dateString) {
|
|
|
106
106
|
const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/;
|
|
107
107
|
return isoRegex.test(dateString) && isValidDate(dateString);
|
|
108
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Check if two dates are in the same week (ISO week, Monday-Sunday)
|
|
111
|
+
* @param date1 - first date
|
|
112
|
+
* @param date2 - second date
|
|
113
|
+
*/
|
|
114
|
+
export function isSameWeek(date1, date2) {
|
|
115
|
+
const getWeekStart = (d) => {
|
|
116
|
+
const date = new Date(d);
|
|
117
|
+
const day = date.getDay();
|
|
118
|
+
const diff = date.getDate() - day + (day === 0 ? -6 : 1); // Adjust for Monday start
|
|
119
|
+
date.setDate(diff);
|
|
120
|
+
date.setHours(0, 0, 0, 0);
|
|
121
|
+
return date;
|
|
122
|
+
};
|
|
123
|
+
return getWeekStart(date1).getTime() === getWeekStart(date2).getTime();
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Check if two dates are in the same month
|
|
127
|
+
* @param date1 - first date
|
|
128
|
+
* @param date2 - second date
|
|
129
|
+
*/
|
|
130
|
+
export function isSameMonth(date1, date2) {
|
|
131
|
+
return (date1.getMonth() === date2.getMonth() &&
|
|
132
|
+
date1.getFullYear() === date2.getFullYear());
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check if two dates are in the same year
|
|
136
|
+
* @param date1 - first date
|
|
137
|
+
* @param date2 - second date
|
|
138
|
+
*/
|
|
139
|
+
export function isSameYear(date1, date2) {
|
|
140
|
+
return date1.getFullYear() === date2.getFullYear();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Check if a date is in the current week
|
|
144
|
+
* @param date - date to check
|
|
145
|
+
*/
|
|
146
|
+
export function isThisWeek(date) {
|
|
147
|
+
return isSameWeek(date, new Date());
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Check if a date is in the current month
|
|
151
|
+
* @param date - date to check
|
|
152
|
+
*/
|
|
153
|
+
export function isThisMonth(date) {
|
|
154
|
+
return isSameMonth(date, new Date());
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Check if a date is in the current year
|
|
158
|
+
* @param date - date to check
|
|
159
|
+
*/
|
|
160
|
+
export function isThisYear(date) {
|
|
161
|
+
return isSameYear(date, new Date());
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Check if a date is a business day (weekday, optionally excluding holidays)
|
|
165
|
+
* @param date - date to check
|
|
166
|
+
* @param holidays - optional array of holiday dates to exclude
|
|
167
|
+
*/
|
|
168
|
+
export function isBusinessDay(date, holidays = []) {
|
|
169
|
+
if (isWeekend(date))
|
|
170
|
+
return false;
|
|
171
|
+
return !holidays.some(holiday => isSameDay(date, holiday));
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Check if a date is in the last N days (including today)
|
|
175
|
+
* @param date - date to check
|
|
176
|
+
* @param n - number of days
|
|
177
|
+
*/
|
|
178
|
+
export function isInLastNDays(date, n) {
|
|
179
|
+
const now = new Date();
|
|
180
|
+
const nDaysAgo = new Date(now);
|
|
181
|
+
nDaysAgo.setDate(nDaysAgo.getDate() - n);
|
|
182
|
+
nDaysAgo.setHours(0, 0, 0, 0);
|
|
183
|
+
const endOfToday = new Date(now);
|
|
184
|
+
endOfToday.setHours(23, 59, 59, 999);
|
|
185
|
+
return date >= nDaysAgo && date <= endOfToday;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Check if a date is in the next N days (including today)
|
|
189
|
+
* @param date - date to check
|
|
190
|
+
* @param n - number of days
|
|
191
|
+
*/
|
|
192
|
+
export function isInNextNDays(date, n) {
|
|
193
|
+
const now = new Date();
|
|
194
|
+
const startOfToday = new Date(now);
|
|
195
|
+
startOfToday.setHours(0, 0, 0, 0);
|
|
196
|
+
const nDaysFromNow = new Date(now);
|
|
197
|
+
nDaysFromNow.setDate(nDaysFromNow.getDate() + n);
|
|
198
|
+
nDaysFromNow.setHours(23, 59, 59, 999);
|
|
199
|
+
return date >= startOfToday && date <= nDaysFromNow;
|
|
200
|
+
}
|