ts-time-utils 1.1.0 → 3.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.
Files changed (92) hide show
  1. package/README.md +567 -12
  2. package/dist/calculate.d.ts +7 -2
  3. package/dist/calculate.d.ts.map +1 -1
  4. package/dist/calculate.js +13 -3
  5. package/dist/calendar.d.ts +103 -0
  6. package/dist/calendar.d.ts.map +1 -1
  7. package/dist/calendar.js +224 -0
  8. package/dist/chain.d.ts +269 -0
  9. package/dist/chain.d.ts.map +1 -0
  10. package/dist/chain.js +422 -0
  11. package/dist/compare.d.ts +217 -0
  12. package/dist/compare.d.ts.map +1 -0
  13. package/dist/compare.js +417 -0
  14. package/dist/cron.d.ts +82 -0
  15. package/dist/cron.d.ts.map +1 -0
  16. package/dist/cron.js +294 -0
  17. package/dist/esm/calculate.d.ts +7 -2
  18. package/dist/esm/calculate.d.ts.map +1 -1
  19. package/dist/esm/calculate.js +13 -3
  20. package/dist/esm/calendar.d.ts +103 -0
  21. package/dist/esm/calendar.d.ts.map +1 -1
  22. package/dist/esm/calendar.js +224 -0
  23. package/dist/esm/chain.d.ts +269 -0
  24. package/dist/esm/chain.d.ts.map +1 -0
  25. package/dist/esm/chain.js +422 -0
  26. package/dist/esm/compare.d.ts +217 -0
  27. package/dist/esm/compare.d.ts.map +1 -0
  28. package/dist/esm/compare.js +417 -0
  29. package/dist/esm/cron.d.ts +82 -0
  30. package/dist/esm/cron.d.ts.map +1 -0
  31. package/dist/esm/cron.js +294 -0
  32. package/dist/esm/fiscal.d.ts +195 -0
  33. package/dist/esm/fiscal.d.ts.map +1 -0
  34. package/dist/esm/fiscal.js +295 -0
  35. package/dist/esm/format.d.ts +65 -0
  36. package/dist/esm/format.d.ts.map +1 -1
  37. package/dist/esm/format.js +202 -0
  38. package/dist/esm/holidays.d.ts +62 -0
  39. package/dist/esm/holidays.d.ts.map +1 -0
  40. package/dist/esm/holidays.js +793 -0
  41. package/dist/esm/index.d.ts +18 -6
  42. package/dist/esm/index.d.ts.map +1 -1
  43. package/dist/esm/index.js +20 -6
  44. package/dist/esm/iterate.d.ts +212 -0
  45. package/dist/esm/iterate.d.ts.map +1 -0
  46. package/dist/esm/iterate.js +409 -0
  47. package/dist/esm/parse.d.ts +45 -0
  48. package/dist/esm/parse.d.ts.map +1 -1
  49. package/dist/esm/parse.js +207 -0
  50. package/dist/esm/plugins.d.ts +129 -0
  51. package/dist/esm/plugins.d.ts.map +1 -0
  52. package/dist/esm/plugins.js +173 -0
  53. package/dist/esm/timezone.d.ts +52 -0
  54. package/dist/esm/timezone.d.ts.map +1 -1
  55. package/dist/esm/timezone.js +171 -0
  56. package/dist/esm/validate.d.ts +51 -0
  57. package/dist/esm/validate.d.ts.map +1 -1
  58. package/dist/esm/validate.js +92 -0
  59. package/dist/esm/workingHours.d.ts +70 -0
  60. package/dist/esm/workingHours.d.ts.map +1 -1
  61. package/dist/esm/workingHours.js +161 -0
  62. package/dist/fiscal.d.ts +195 -0
  63. package/dist/fiscal.d.ts.map +1 -0
  64. package/dist/fiscal.js +295 -0
  65. package/dist/format.d.ts +65 -0
  66. package/dist/format.d.ts.map +1 -1
  67. package/dist/format.js +202 -0
  68. package/dist/holidays.d.ts +62 -0
  69. package/dist/holidays.d.ts.map +1 -0
  70. package/dist/holidays.js +793 -0
  71. package/dist/index.d.ts +18 -6
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +20 -6
  74. package/dist/iterate.d.ts +212 -0
  75. package/dist/iterate.d.ts.map +1 -0
  76. package/dist/iterate.js +409 -0
  77. package/dist/parse.d.ts +45 -0
  78. package/dist/parse.d.ts.map +1 -1
  79. package/dist/parse.js +207 -0
  80. package/dist/plugins.d.ts +129 -0
  81. package/dist/plugins.d.ts.map +1 -0
  82. package/dist/plugins.js +173 -0
  83. package/dist/timezone.d.ts +52 -0
  84. package/dist/timezone.d.ts.map +1 -1
  85. package/dist/timezone.js +171 -0
  86. package/dist/validate.d.ts +51 -0
  87. package/dist/validate.d.ts.map +1 -1
  88. package/dist/validate.js +92 -0
  89. package/dist/workingHours.d.ts +70 -0
  90. package/dist/workingHours.d.ts.map +1 -1
  91. package/dist/workingHours.js +161 -0
  92. package/package.json +40 -1
@@ -37,9 +37,14 @@ export declare function endOf(date: Date, unit: 'day' | 'week' | 'month' | 'year
37
37
  * Check if a date is between two other dates
38
38
  * @param date - date to check
39
39
  * @param start - start date (inclusive)
40
- * @param end - end date (inclusive)
40
+ /**
41
+ * Check if a date falls between two dates
42
+ * @param date - date to check
43
+ * @param start - start date
44
+ * @param end - end date
45
+ * @param inclusive - whether boundaries are inclusive (default: true)
41
46
  */
42
- export declare function isBetween(date: Date, start: Date, end: Date): boolean;
47
+ export declare function isBetween(date: Date, start: Date, end: Date, inclusive?: boolean): boolean;
43
48
  /**
44
49
  * Get the number of business days between two dates (excludes weekends)
45
50
  * @param startDate - start date
@@ -1 +1 @@
1
- {"version":3,"file":"calculate.d.ts","sourceRoot":"","sources":["../src/calculate.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,QAAQ,EACT,MAAM,gBAAgB,CAAC;AAExB;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,IAAI,EACX,KAAK,EAAE,IAAI,EACX,IAAI,GAAE,QAAyB,EAC/B,OAAO,GAAE,OAAc,GACtB,MAAM,CAkCR;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAgDxE;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAE7E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MA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AO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4BnG;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,SAAS,GAAE,OAAc,GAAG,OAAO,CAShG;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,GAAG,MAAM,CAa1E"}
package/dist/calculate.js CHANGED
@@ -168,11 +168,21 @@ export function endOf(date, unit) {
168
168
  * Check if a date is between two other dates
169
169
  * @param date - date to check
170
170
  * @param start - start date (inclusive)
171
- * @param end - end date (inclusive)
171
+ /**
172
+ * Check if a date falls between two dates
173
+ * @param date - date to check
174
+ * @param start - start date
175
+ * @param end - end date
176
+ * @param inclusive - whether boundaries are inclusive (default: true)
172
177
  */
173
- export function isBetween(date, start, end) {
178
+ export function isBetween(date, start, end, inclusive = true) {
174
179
  const time = date.getTime();
175
- return time >= start.getTime() && time <= end.getTime();
180
+ const startTime = start.getTime();
181
+ const endTime = end.getTime();
182
+ if (inclusive) {
183
+ return time >= startTime && time <= endTime;
184
+ }
185
+ return time > startTime && time < endTime;
176
186
  }
177
187
  /**
178
188
  * Get the number of business days between two dates (excludes weekends)
@@ -79,4 +79,107 @@ export declare function getFirstDayOfYear(year: number): Date;
79
79
  * @param year - year
80
80
  */
81
81
  export declare function getLastDayOfYear(year: number): Date;
82
+ /**
83
+ * Get the nth occurrence of a day in a month (e.g., 2nd Monday)
84
+ * @param year - year
85
+ * @param month - month (0-11)
86
+ * @param dayOfWeek - day of week (0=Sunday, 6=Saturday)
87
+ * @param n - occurrence (1-5, or -1 for last)
88
+ */
89
+ export declare function getNthDayOfMonth(year: number, month: number, dayOfWeek: number, n: number): Date | null;
90
+ /**
91
+ * US Holiday type
92
+ */
93
+ export interface USHoliday {
94
+ name: string;
95
+ date: Date;
96
+ type: 'federal' | 'observance';
97
+ }
98
+ /**
99
+ * Get New Year's Day
100
+ * @param year - year
101
+ */
102
+ export declare function getNewYearsDay(year: number): Date;
103
+ /**
104
+ * Get Martin Luther King Jr. Day (3rd Monday of January)
105
+ * @param year - year
106
+ */
107
+ export declare function getMLKDay(year: number): Date | null;
108
+ /**
109
+ * Get Presidents' Day (3rd Monday of February)
110
+ * @param year - year
111
+ */
112
+ export declare function getPresidentsDay(year: number): Date | null;
113
+ /**
114
+ * Get Memorial Day (last Monday of May)
115
+ * @param year - year
116
+ */
117
+ export declare function getMemorialDay(year: number): Date | null;
118
+ /**
119
+ * Get Independence Day (July 4th)
120
+ * @param year - year
121
+ */
122
+ export declare function getIndependenceDay(year: number): Date;
123
+ /**
124
+ * Get Labor Day (1st Monday of September)
125
+ * @param year - year
126
+ */
127
+ export declare function getLaborDay(year: number): Date | null;
128
+ /**
129
+ * Get Columbus Day (2nd Monday of October)
130
+ * @param year - year
131
+ */
132
+ export declare function getColumbusDay(year: number): Date | null;
133
+ /**
134
+ * Get Veterans Day (November 11th)
135
+ * @param year - year
136
+ */
137
+ export declare function getVeteransDay(year: number): Date;
138
+ /**
139
+ * Get Thanksgiving Day (4th Thursday of November)
140
+ * @param year - year
141
+ */
142
+ export declare function getThanksgivingDay(year: number): Date | null;
143
+ /**
144
+ * Get Christmas Day (December 25th)
145
+ * @param year - year
146
+ */
147
+ export declare function getChristmasDay(year: number): Date;
148
+ /**
149
+ * Get Good Friday (Friday before Easter)
150
+ * @param year - year
151
+ */
152
+ export declare function getGoodFriday(year: number): Date;
153
+ /**
154
+ * Get all US federal holidays for a year
155
+ * @param year - year
156
+ */
157
+ export declare function getUSHolidays(year: number): USHoliday[];
158
+ /**
159
+ * Check if a date is a US federal holiday
160
+ * @param date - date to check
161
+ */
162
+ export declare function isUSHoliday(date: Date): boolean;
163
+ /**
164
+ * Get the name of a US holiday for a given date
165
+ * @param date - date to check
166
+ * @returns holiday name or null if not a holiday
167
+ */
168
+ export declare function getUSHolidayName(date: Date): string | null;
169
+ /**
170
+ * Get the start of the week for a date (Monday)
171
+ * @param date - any date
172
+ */
173
+ export declare function getStartOfWeek(date: Date): Date;
174
+ /**
175
+ * Get the end of the week for a date (Sunday)
176
+ * @param date - any date
177
+ */
178
+ export declare function getEndOfWeek(date: Date): Date;
179
+ /**
180
+ * Get all weeks in a month as arrays of dates
181
+ * @param year - year
182
+ * @param month - month (0-11)
183
+ */
184
+ export declare function getWeeksInMonth(year: number, month: number): Date[][];
82
185
  //# sourceMappingURL=calendar.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"calendar.d.ts","sourceRoot":"","sources":["../src/calendar.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAMhD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAKjD;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE7C;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAI/C;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAYnD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAUD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAkB5C;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,CAEpD;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAA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AAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAErD;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAErD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAExD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAEjD;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAE5D;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAElD;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAKhD;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,CA8BvD;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAO/C;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,CAQ1D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAO/C;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAM7C;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,EAAE,CAkCrE"}
package/dist/calendar.js CHANGED
@@ -152,3 +152,227 @@ export function getFirstDayOfYear(year) {
152
152
  export function getLastDayOfYear(year) {
153
153
  return new Date(year, 11, 31);
154
154
  }
155
+ /**
156
+ * Get the nth occurrence of a day in a month (e.g., 2nd Monday)
157
+ * @param year - year
158
+ * @param month - month (0-11)
159
+ * @param dayOfWeek - day of week (0=Sunday, 6=Saturday)
160
+ * @param n - occurrence (1-5, or -1 for last)
161
+ */
162
+ export function getNthDayOfMonth(year, month, dayOfWeek, n) {
163
+ if (n === 0 || n < -1 || n > 5)
164
+ return null;
165
+ const firstDay = new Date(year, month, 1);
166
+ const lastDay = new Date(year, month + 1, 0);
167
+ if (n === -1) {
168
+ // Last occurrence
169
+ let date = lastDay.getDate();
170
+ while (date > 0) {
171
+ const d = new Date(year, month, date);
172
+ if (d.getDay() === dayOfWeek)
173
+ return d;
174
+ date--;
175
+ }
176
+ return null;
177
+ }
178
+ // Find nth occurrence
179
+ let count = 0;
180
+ for (let date = 1; date <= lastDay.getDate(); date++) {
181
+ const d = new Date(year, month, date);
182
+ if (d.getDay() === dayOfWeek) {
183
+ count++;
184
+ if (count === n)
185
+ return d;
186
+ }
187
+ }
188
+ return null;
189
+ }
190
+ /**
191
+ * Get New Year's Day
192
+ * @param year - year
193
+ */
194
+ export function getNewYearsDay(year) {
195
+ return new Date(year, 0, 1);
196
+ }
197
+ /**
198
+ * Get Martin Luther King Jr. Day (3rd Monday of January)
199
+ * @param year - year
200
+ */
201
+ export function getMLKDay(year) {
202
+ return getNthDayOfMonth(year, 0, 1, 3);
203
+ }
204
+ /**
205
+ * Get Presidents' Day (3rd Monday of February)
206
+ * @param year - year
207
+ */
208
+ export function getPresidentsDay(year) {
209
+ return getNthDayOfMonth(year, 1, 1, 3);
210
+ }
211
+ /**
212
+ * Get Memorial Day (last Monday of May)
213
+ * @param year - year
214
+ */
215
+ export function getMemorialDay(year) {
216
+ return getNthDayOfMonth(year, 4, 1, -1);
217
+ }
218
+ /**
219
+ * Get Independence Day (July 4th)
220
+ * @param year - year
221
+ */
222
+ export function getIndependenceDay(year) {
223
+ return new Date(year, 6, 4);
224
+ }
225
+ /**
226
+ * Get Labor Day (1st Monday of September)
227
+ * @param year - year
228
+ */
229
+ export function getLaborDay(year) {
230
+ return getNthDayOfMonth(year, 8, 1, 1);
231
+ }
232
+ /**
233
+ * Get Columbus Day (2nd Monday of October)
234
+ * @param year - year
235
+ */
236
+ export function getColumbusDay(year) {
237
+ return getNthDayOfMonth(year, 9, 1, 2);
238
+ }
239
+ /**
240
+ * Get Veterans Day (November 11th)
241
+ * @param year - year
242
+ */
243
+ export function getVeteransDay(year) {
244
+ return new Date(year, 10, 11);
245
+ }
246
+ /**
247
+ * Get Thanksgiving Day (4th Thursday of November)
248
+ * @param year - year
249
+ */
250
+ export function getThanksgivingDay(year) {
251
+ return getNthDayOfMonth(year, 10, 4, 4);
252
+ }
253
+ /**
254
+ * Get Christmas Day (December 25th)
255
+ * @param year - year
256
+ */
257
+ export function getChristmasDay(year) {
258
+ return new Date(year, 11, 25);
259
+ }
260
+ /**
261
+ * Get Good Friday (Friday before Easter)
262
+ * @param year - year
263
+ */
264
+ export function getGoodFriday(year) {
265
+ const easter = getEaster(year);
266
+ const goodFriday = new Date(easter);
267
+ goodFriday.setDate(easter.getDate() - 2);
268
+ return goodFriday;
269
+ }
270
+ /**
271
+ * Get all US federal holidays for a year
272
+ * @param year - year
273
+ */
274
+ export function getUSHolidays(year) {
275
+ const holidays = [];
276
+ holidays.push({ name: "New Year's Day", date: getNewYearsDay(year), type: 'federal' });
277
+ const mlk = getMLKDay(year);
278
+ if (mlk)
279
+ holidays.push({ name: "Martin Luther King Jr. Day", date: mlk, type: 'federal' });
280
+ const presidents = getPresidentsDay(year);
281
+ if (presidents)
282
+ holidays.push({ name: "Presidents' Day", date: presidents, type: 'federal' });
283
+ const memorial = getMemorialDay(year);
284
+ if (memorial)
285
+ holidays.push({ name: "Memorial Day", date: memorial, type: 'federal' });
286
+ holidays.push({ name: "Independence Day", date: getIndependenceDay(year), type: 'federal' });
287
+ const labor = getLaborDay(year);
288
+ if (labor)
289
+ holidays.push({ name: "Labor Day", date: labor, type: 'federal' });
290
+ const columbus = getColumbusDay(year);
291
+ if (columbus)
292
+ holidays.push({ name: "Columbus Day", date: columbus, type: 'federal' });
293
+ holidays.push({ name: "Veterans Day", date: getVeteransDay(year), type: 'federal' });
294
+ const thanksgiving = getThanksgivingDay(year);
295
+ if (thanksgiving)
296
+ holidays.push({ name: "Thanksgiving Day", date: thanksgiving, type: 'federal' });
297
+ holidays.push({ name: "Christmas Day", date: getChristmasDay(year), type: 'federal' });
298
+ return holidays.sort((a, b) => a.date.getTime() - b.date.getTime());
299
+ }
300
+ /**
301
+ * Check if a date is a US federal holiday
302
+ * @param date - date to check
303
+ */
304
+ export function isUSHoliday(date) {
305
+ const holidays = getUSHolidays(date.getFullYear());
306
+ return holidays.some(h => h.date.getFullYear() === date.getFullYear() &&
307
+ h.date.getMonth() === date.getMonth() &&
308
+ h.date.getDate() === date.getDate());
309
+ }
310
+ /**
311
+ * Get the name of a US holiday for a given date
312
+ * @param date - date to check
313
+ * @returns holiday name or null if not a holiday
314
+ */
315
+ export function getUSHolidayName(date) {
316
+ const holidays = getUSHolidays(date.getFullYear());
317
+ const holiday = holidays.find(h => h.date.getFullYear() === date.getFullYear() &&
318
+ h.date.getMonth() === date.getMonth() &&
319
+ h.date.getDate() === date.getDate());
320
+ return holiday ? holiday.name : null;
321
+ }
322
+ /**
323
+ * Get the start of the week for a date (Monday)
324
+ * @param date - any date
325
+ */
326
+ export function getStartOfWeek(date) {
327
+ const d = new Date(date);
328
+ const day = d.getDay();
329
+ const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Adjust for Monday start
330
+ d.setDate(diff);
331
+ d.setHours(0, 0, 0, 0);
332
+ return d;
333
+ }
334
+ /**
335
+ * Get the end of the week for a date (Sunday)
336
+ * @param date - any date
337
+ */
338
+ export function getEndOfWeek(date) {
339
+ const start = getStartOfWeek(date);
340
+ const end = new Date(start);
341
+ end.setDate(start.getDate() + 6);
342
+ end.setHours(23, 59, 59, 999);
343
+ return end;
344
+ }
345
+ /**
346
+ * Get all weeks in a month as arrays of dates
347
+ * @param year - year
348
+ * @param month - month (0-11)
349
+ */
350
+ export function getWeeksInMonth(year, month) {
351
+ const weeks = [];
352
+ const firstDay = new Date(year, month, 1);
353
+ const lastDay = new Date(year, month + 1, 0);
354
+ let currentWeek = [];
355
+ const startDayOfWeek = firstDay.getDay() || 7; // Convert Sunday from 0 to 7 for ISO
356
+ // Add days from previous month to fill the first week
357
+ for (let i = 1; i < startDayOfWeek; i++) {
358
+ const prevDate = new Date(year, month, 1 - (startDayOfWeek - i));
359
+ currentWeek.push(prevDate);
360
+ }
361
+ // Add days of the current month
362
+ for (let day = 1; day <= lastDay.getDate(); day++) {
363
+ currentWeek.push(new Date(year, month, day));
364
+ if (currentWeek.length === 7) {
365
+ weeks.push(currentWeek);
366
+ currentWeek = [];
367
+ }
368
+ }
369
+ // Add days from next month to fill the last week
370
+ if (currentWeek.length > 0) {
371
+ let nextDay = 1;
372
+ while (currentWeek.length < 7) {
373
+ currentWeek.push(new Date(year, month + 1, nextDay++));
374
+ }
375
+ weeks.push(currentWeek);
376
+ }
377
+ return weeks;
378
+ }
@@ -0,0 +1,269 @@
1
+ /**
2
+ * Fluent chain API for ts-time-utils
3
+ *
4
+ * Provides a chainable interface for date operations:
5
+ * ```ts
6
+ * chain(new Date())
7
+ * .add(1, 'day')
8
+ * .startOf('month')
9
+ * .format('YYYY-MM-DD')
10
+ * ```
11
+ */
12
+ import type { TimeUnit, DateInput, FormatOptions } from './types.js';
13
+ /** Units accepted by startOf/endOf */
14
+ type StartOfUnit = 'day' | 'week' | 'month' | 'year' | 'hour' | 'minute';
15
+ /**
16
+ * Immutable chainable date wrapper
17
+ * All transformation methods return a new ChainedDate instance
18
+ */
19
+ export declare class ChainedDate {
20
+ private readonly _date;
21
+ constructor(date?: DateInput);
22
+ /**
23
+ * Add time to the date
24
+ * @example chain(date).add(1, 'day').add(2, 'hours')
25
+ */
26
+ add(amount: number, unit: TimeUnit): ChainedDate;
27
+ /**
28
+ * Subtract time from the date
29
+ * @example chain(date).subtract(1, 'week')
30
+ */
31
+ subtract(amount: number, unit: TimeUnit): ChainedDate;
32
+ /**
33
+ * Get the start of a time period
34
+ * @example chain(date).startOf('month')
35
+ */
36
+ startOf(unit: StartOfUnit): ChainedDate;
37
+ /**
38
+ * Get the end of a time period
39
+ * @example chain(date).endOf('day')
40
+ */
41
+ endOf(unit: StartOfUnit): ChainedDate;
42
+ /**
43
+ * Set specific date/time components
44
+ * @example chain(date).set({ year: 2025, month: 1 })
45
+ */
46
+ set(values: {
47
+ year?: number;
48
+ month?: number;
49
+ day?: number;
50
+ hours?: number;
51
+ minutes?: number;
52
+ seconds?: number;
53
+ milliseconds?: number;
54
+ }): ChainedDate;
55
+ /**
56
+ * Clone the ChainedDate
57
+ */
58
+ clone(): ChainedDate;
59
+ /**
60
+ * Format date using pattern string
61
+ * @example chain(date).format('YYYY-MM-DD') // "2025-01-15"
62
+ */
63
+ format(pattern: string): string;
64
+ /**
65
+ * Format time in 12h, 24h, or ISO format
66
+ * @example chain(date).formatTime('12h') // "2:30 PM"
67
+ */
68
+ formatTime(fmt?: '12h' | '24h' | 'iso'): string;
69
+ /**
70
+ * Format as calendar date (Today, Yesterday, Monday, etc.)
71
+ * @example chain(date).calendar() // "Tomorrow"
72
+ */
73
+ calendar(): string;
74
+ /**
75
+ * Get relative time string
76
+ * @example chain(pastDate).ago() // "3 hours ago"
77
+ */
78
+ ago(options?: FormatOptions): string;
79
+ /**
80
+ * Get ISO string
81
+ */
82
+ toISOString(): string;
83
+ /**
84
+ * Get locale string
85
+ */
86
+ toLocaleString(locale?: string, options?: Intl.DateTimeFormatOptions): string;
87
+ /**
88
+ * Format day as ordinal
89
+ * @example chain(date).dayOrdinal() // "15th"
90
+ */
91
+ dayOrdinal(): string;
92
+ /**
93
+ * Check if date is valid
94
+ */
95
+ isValid(): boolean;
96
+ /**
97
+ * Check if date is today
98
+ */
99
+ isToday(): boolean;
100
+ /**
101
+ * Check if date is yesterday
102
+ */
103
+ isYesterday(): boolean;
104
+ /**
105
+ * Check if date is tomorrow
106
+ */
107
+ isTomorrow(): boolean;
108
+ /**
109
+ * Check if date is in the past
110
+ */
111
+ isPast(): boolean;
112
+ /**
113
+ * Check if date is in the future
114
+ */
115
+ isFuture(): boolean;
116
+ /**
117
+ * Check if date is a weekend
118
+ */
119
+ isWeekend(): boolean;
120
+ /**
121
+ * Check if date is a weekday
122
+ */
123
+ isWeekday(): boolean;
124
+ /**
125
+ * Check if date is in this week
126
+ */
127
+ isThisWeek(): boolean;
128
+ /**
129
+ * Check if date is in this month
130
+ */
131
+ isThisMonth(): boolean;
132
+ /**
133
+ * Check if date is in this year
134
+ */
135
+ isThisYear(): boolean;
136
+ /**
137
+ * Check if year is a leap year
138
+ */
139
+ isLeapYear(): boolean;
140
+ /**
141
+ * Check if date is a business day
142
+ */
143
+ isBusinessDay(holidays?: Date[]): boolean;
144
+ /**
145
+ * Check if date is same day as another
146
+ */
147
+ isSameDay(other: DateInput): boolean;
148
+ /**
149
+ * Check if date is same week as another
150
+ */
151
+ isSameWeek(other: DateInput): boolean;
152
+ /**
153
+ * Check if date is same month as another
154
+ */
155
+ isSameMonth(other: DateInput): boolean;
156
+ /**
157
+ * Check if date is same year as another
158
+ */
159
+ isSameYear(other: DateInput): boolean;
160
+ /**
161
+ * Check if date is before another
162
+ */
163
+ isBefore(other: DateInput): boolean;
164
+ /**
165
+ * Check if date is after another
166
+ */
167
+ isAfter(other: DateInput): boolean;
168
+ /**
169
+ * Check if date is between two dates
170
+ */
171
+ isBetween(start: DateInput, end: DateInput, inclusive?: boolean): boolean;
172
+ /**
173
+ * Get difference from another date
174
+ * @example chain(date).diff(other, 'days') // 5
175
+ */
176
+ diff(other: DateInput, unit?: TimeUnit, precise?: boolean): number;
177
+ /**
178
+ * Get the timestamp (milliseconds since epoch)
179
+ */
180
+ valueOf(): number;
181
+ /**
182
+ * Get year
183
+ */
184
+ year(): number;
185
+ /**
186
+ * Get month (1-12)
187
+ */
188
+ month(): number;
189
+ /**
190
+ * Get day of month (1-31)
191
+ */
192
+ day(): number;
193
+ /**
194
+ * Get day of week (0-6, 0=Sunday)
195
+ */
196
+ weekday(): number;
197
+ /**
198
+ * Get hours (0-23)
199
+ */
200
+ hours(): number;
201
+ /**
202
+ * Get minutes (0-59)
203
+ */
204
+ minutes(): number;
205
+ /**
206
+ * Get seconds (0-59)
207
+ */
208
+ seconds(): number;
209
+ /**
210
+ * Get milliseconds (0-999)
211
+ */
212
+ milliseconds(): number;
213
+ /**
214
+ * Get day of year (1-366)
215
+ */
216
+ dayOfYear(): number;
217
+ /**
218
+ * Get ISO week number (1-53)
219
+ */
220
+ week(): number;
221
+ /**
222
+ * Get quarter (1-4)
223
+ */
224
+ quarter(): number;
225
+ /**
226
+ * Get days in month
227
+ */
228
+ daysInMonth(): number;
229
+ /**
230
+ * Get the underlying Date object
231
+ */
232
+ toDate(): Date;
233
+ /**
234
+ * Get Unix timestamp (seconds)
235
+ */
236
+ unix(): number;
237
+ /**
238
+ * Convert to array [year, month, day, hours, minutes, seconds, ms]
239
+ */
240
+ toArray(): [number, number, number, number, number, number, number];
241
+ /**
242
+ * Convert to object
243
+ */
244
+ toObject(): {
245
+ year: number;
246
+ month: number;
247
+ day: number;
248
+ hours: number;
249
+ minutes: number;
250
+ seconds: number;
251
+ milliseconds: number;
252
+ };
253
+ }
254
+ /**
255
+ * Create a chainable date wrapper
256
+ * @example
257
+ * chain(new Date()).add(1, 'day').format('YYYY-MM-DD')
258
+ * chain('2025-01-15').startOf('month').toDate()
259
+ * chain().add(1, 'week').isWeekend()
260
+ */
261
+ export declare function chain(date?: DateInput): ChainedDate;
262
+ /**
263
+ * Format a duration in milliseconds
264
+ * Convenience export for use with chain().diff()
265
+ * @example formatMs(chain(a).diff(b)) // "2 days, 3 hours"
266
+ */
267
+ export declare function formatMs(ms: number, options?: FormatOptions): string;
268
+ export {};
269
+ //# sourceMappingURL=chain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chain.d.ts","sourceRoot":"","sources":["../src/chain.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG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