tiny-ja-date 0.4.0 → 0.6.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 CHANGED
@@ -7,6 +7,8 @@ Tiny TypeScript date utilities for Japanese UI.
7
7
  - 土日判定
8
8
  - 営業日判定
9
9
  - 次の営業日計算
10
+ - 営業日数の加算・減算
11
+ - 2つの日付間の営業日数
10
12
 
11
13
  ## Install
12
14
 
@@ -18,6 +20,8 @@ npm install tiny-ja-date
18
20
 
19
21
  ```ts
20
22
  import {
23
+ addBusinessDays,
24
+ countBusinessDays,
21
25
  formatJaDate,
22
26
  formatRelativeJaDate,
23
27
  isBusinessDay,
@@ -39,6 +43,12 @@ isBusinessDay("2026-05-18", { holidays: ["2026-05-18"] })
39
43
 
40
44
  nextBusinessDay(new Date(2026, 4, 15))
41
45
  // Date for 2026-05-18
46
+
47
+ addBusinessDays("2026-05-15", 2)
48
+ // Date for 2026-05-19
49
+
50
+ countBusinessDays("2026-05-15", "2026-05-19")
51
+ // 2
42
52
  ```
43
53
 
44
54
  ## API
@@ -117,6 +127,50 @@ nextBusinessDay("2026-05-15", { holidays: ["2026-05-18"] })
117
127
  // Date for 2026-05-19
118
128
  ```
119
129
 
130
+ ### `addBusinessDays(date: DateInput, amount: number, options?: BusinessDayOptions): Date`
131
+
132
+ Counts forward for a positive amount and backward for a negative amount. Counting
133
+ starts the day after or before the input and skips Saturdays, Sundays, dates in
134
+ `options.holidays`, and dates skipped entirely by the time zone.
135
+ `addBusinessDays(date, 1, options)` is equivalent to `nextBusinessDay(date, options)`.
136
+
137
+ Zero returns the start of the input date, including a weekend or holiday.
138
+ When midnight is skipped, the result is the first valid time on that date.
139
+ If the entire input date is skipped, zero throws a `TypeError`. Holiday inputs
140
+ are validated for every amount, including zero. An amount that is not a safe
141
+ integer throws a `RangeError`. A result beyond the `Date` range throws a `TypeError`.
142
+
143
+ ```ts
144
+ const options = { timeZone: "Asia/Tokyo", holidays: ["2026-11-23"] }
145
+
146
+ addBusinessDays("2026-11-20", 2, options)
147
+ // Start of 2026-11-25 in Tokyo
148
+
149
+ addBusinessDays("2026-11-24", -1, options)
150
+ // Start of 2026-11-20 in Tokyo
151
+ ```
152
+
153
+ ### `countBusinessDays(from: DateInput, to: DateInput, options?: BusinessDayOptions): number`
154
+
155
+ Counts from the day after or before `from` through `to` in the selected time
156
+ zone. The starting date is excluded, and the destination counts if it is a
157
+ business day. Counts are positive going forward and negative going backward.
158
+ The same calendar date or a range with no business days returns `0`.
159
+
160
+ Weekends, dates in `options.holidays`, and dates skipped entirely by the time
161
+ zone do not count. Both endpoints and all holiday inputs are validated even
162
+ when the result is `0`.
163
+
164
+ ```ts
165
+ const options = { timeZone: "Asia/Tokyo", holidays: ["2026-11-23"] }
166
+
167
+ countBusinessDays("2026-11-20", "2026-11-25", options) // 2
168
+ countBusinessDays("2026-11-25", "2026-11-20", options) // -2
169
+
170
+ countBusinessDays("2026-11-20", "2026-11-22", options) // 0
171
+ countBusinessDays("2026-11-22", "2026-11-20", options) // -1
172
+ ```
173
+
120
174
  ## Time zones
121
175
 
122
176
  All public functions accept an optional `timeZone` such as `"Asia/Tokyo"`.
package/dist/index.d.ts CHANGED
@@ -9,4 +9,6 @@ export declare function formatJaDate(input: DateInput, options?: DateOptions): s
9
9
  export declare function formatRelativeJaDate(input: DateInput, baseInput?: DateInput, options?: DateOptions): string;
10
10
  export declare function isWeekendJP(input: DateInput, options?: DateOptions): boolean;
11
11
  export declare function isBusinessDay(input: DateInput, options?: BusinessDayOptions): boolean;
12
+ export declare function addBusinessDays(input: DateInput, amount: number, options?: BusinessDayOptions): Date;
12
13
  export declare function nextBusinessDay(input: DateInput, options?: BusinessDayOptions): Date;
14
+ export declare function countBusinessDays(from: DateInput, to: DateInput, options?: BusinessDayOptions): number;
package/dist/index.js CHANGED
@@ -147,16 +147,19 @@ function isWeekend(day) {
147
147
  function holidayDays(holidays, formatter) {
148
148
  return new Set(holidays?.map((holiday) => calendarDay(holiday, formatter)));
149
149
  }
150
- function businessDayStart(candidate, holidays, formatter) {
151
- if (isWeekend(candidate) || holidays.has(candidate))
152
- return undefined;
150
+ function dayStart(day, formatter) {
153
151
  if (formatter)
154
- return zonedDayStart(candidate, formatter);
155
- const { year, month, day } = utcDateParts(candidate);
152
+ return zonedDayStart(day, formatter);
153
+ const { year, month, day: dayOfMonth } = utcDateParts(day);
156
154
  const local = new Date(0);
157
155
  local.setHours(0, 0, 0, 0);
158
- local.setFullYear(year, month - 1, day);
159
- return local.getDate() === day ? local : undefined;
156
+ local.setFullYear(year, month - 1, dayOfMonth);
157
+ return local.getDate() === dayOfMonth ? local : undefined;
158
+ }
159
+ function businessDayStart(candidate, holidays, formatter) {
160
+ if (isWeekend(candidate) || holidays.has(candidate))
161
+ return undefined;
162
+ return dayStart(candidate, formatter);
160
163
  }
161
164
  export function formatJaDate(input, options = {}) {
162
165
  const formatter = createFormatter(options.timeZone);
@@ -187,14 +190,51 @@ export function isBusinessDay(input, options = {}) {
187
190
  const holidays = holidayDays(options.holidays, formatter);
188
191
  return businessDayStart(day, holidays, formatter) !== undefined;
189
192
  }
190
- export function nextBusinessDay(input, options = {}) {
193
+ export function addBusinessDays(input, amount, options = {}) {
194
+ if (!Number.isSafeInteger(amount))
195
+ throw new RangeError("Expected a safe integer business-day amount");
191
196
  const formatter = createFormatter(options.timeZone);
192
197
  const day = calendarDay(input, formatter);
193
198
  const holidays = holidayDays(options.holidays, formatter);
194
- for (let candidate = day + ONE_DAY_MS; candidate <= MAX_DATE_MS + ONE_DAY_MS; candidate += ONE_DAY_MS) {
199
+ if (amount === 0) {
200
+ const start = dayStart(day, formatter);
201
+ if (!start)
202
+ throw new TypeError("Invalid date");
203
+ return start;
204
+ }
205
+ const direction = Math.sign(amount);
206
+ const step = direction * ONE_DAY_MS;
207
+ // A local calendar date can extend one day beyond the UTC Date limits.
208
+ const limit = MAX_DATE_MS + ONE_DAY_MS;
209
+ let remaining = Math.abs(amount);
210
+ if (remaining > (limit - direction * day) / ONE_DAY_MS)
211
+ throw new TypeError("Invalid date");
212
+ for (let candidate = day + step; Math.abs(candidate) <= limit; candidate += step) {
195
213
  const start = businessDayStart(candidate, holidays, formatter);
196
- if (start)
214
+ if (!start)
215
+ continue;
216
+ remaining -= 1;
217
+ if (remaining === 0)
197
218
  return start;
198
219
  }
199
220
  throw new TypeError("Invalid date");
200
221
  }
222
+ export function nextBusinessDay(input, options = {}) {
223
+ return addBusinessDays(input, 1, options);
224
+ }
225
+ export function countBusinessDays(from, to, options = {}) {
226
+ const formatter = createFormatter(options.timeZone);
227
+ const start = dateOnlyDay(from) ?? calendarDay(from, formatter);
228
+ const end = dateOnlyDay(to) ?? calendarDay(to, formatter);
229
+ const holidays = holidayDays(options.holidays, formatter);
230
+ const direction = Math.sign(end - start);
231
+ const step = direction * ONE_DAY_MS;
232
+ let count = 0;
233
+ let day = start;
234
+ while (day !== end) {
235
+ day += step;
236
+ if (businessDayStart(day, holidays, formatter))
237
+ count += direction;
238
+ }
239
+ return count;
240
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-ja-date",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Tiny TypeScript date utilities for Japanese UI.",
5
5
  "type": "module",
6
6
  "exports": {