tiny-ja-date 0.5.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 +26 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +16 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ Tiny TypeScript date utilities for Japanese UI.
|
|
|
8
8
|
- 営業日判定
|
|
9
9
|
- 次の営業日計算
|
|
10
10
|
- 営業日数の加算・減算
|
|
11
|
+
- 2つの日付間の営業日数
|
|
11
12
|
|
|
12
13
|
## Install
|
|
13
14
|
|
|
@@ -20,6 +21,7 @@ npm install tiny-ja-date
|
|
|
20
21
|
```ts
|
|
21
22
|
import {
|
|
22
23
|
addBusinessDays,
|
|
24
|
+
countBusinessDays,
|
|
23
25
|
formatJaDate,
|
|
24
26
|
formatRelativeJaDate,
|
|
25
27
|
isBusinessDay,
|
|
@@ -44,6 +46,9 @@ nextBusinessDay(new Date(2026, 4, 15))
|
|
|
44
46
|
|
|
45
47
|
addBusinessDays("2026-05-15", 2)
|
|
46
48
|
// Date for 2026-05-19
|
|
49
|
+
|
|
50
|
+
countBusinessDays("2026-05-15", "2026-05-19")
|
|
51
|
+
// 2
|
|
47
52
|
```
|
|
48
53
|
|
|
49
54
|
## API
|
|
@@ -145,6 +150,27 @@ addBusinessDays("2026-11-24", -1, options)
|
|
|
145
150
|
// Start of 2026-11-20 in Tokyo
|
|
146
151
|
```
|
|
147
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
|
+
|
|
148
174
|
## Time zones
|
|
149
175
|
|
|
150
176
|
All public functions accept an optional `timeZone` such as `"Asia/Tokyo"`.
|
package/dist/index.d.ts
CHANGED
|
@@ -11,3 +11,4 @@ export declare function isWeekendJP(input: DateInput, options?: DateOptions): bo
|
|
|
11
11
|
export declare function isBusinessDay(input: DateInput, options?: BusinessDayOptions): boolean;
|
|
12
12
|
export declare function addBusinessDays(input: DateInput, amount: number, options?: BusinessDayOptions): Date;
|
|
13
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
|
@@ -222,3 +222,19 @@ export function addBusinessDays(input, amount, options = {}) {
|
|
|
222
222
|
export function nextBusinessDay(input, options = {}) {
|
|
223
223
|
return addBusinessDays(input, 1, options);
|
|
224
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
|
+
}
|