tiny-ja-date 0.3.0 → 0.4.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 +23 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +33 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@ Tiny TypeScript date utilities for Japanese UI.
|
|
|
5
5
|
- 日本語の日付表示
|
|
6
6
|
- 日本語の相対日付表示
|
|
7
7
|
- 土日判定
|
|
8
|
+
- 営業日判定
|
|
8
9
|
- 次の営業日計算
|
|
9
10
|
|
|
10
11
|
## Install
|
|
@@ -19,6 +20,7 @@ npm install tiny-ja-date
|
|
|
19
20
|
import {
|
|
20
21
|
formatJaDate,
|
|
21
22
|
formatRelativeJaDate,
|
|
23
|
+
isBusinessDay,
|
|
22
24
|
isWeekendJP,
|
|
23
25
|
nextBusinessDay,
|
|
24
26
|
} from "tiny-ja-date"
|
|
@@ -32,6 +34,9 @@ formatRelativeJaDate(new Date(2026, 4, 15), new Date(2026, 4, 12))
|
|
|
32
34
|
isWeekendJP(new Date(2026, 4, 16))
|
|
33
35
|
// true
|
|
34
36
|
|
|
37
|
+
isBusinessDay("2026-05-18", { holidays: ["2026-05-18"] })
|
|
38
|
+
// false
|
|
39
|
+
|
|
35
40
|
nextBusinessDay(new Date(2026, 4, 15))
|
|
36
41
|
// Date for 2026-05-18
|
|
37
42
|
```
|
|
@@ -77,6 +82,23 @@ isWeekendJP(new Date(2026, 4, 16))
|
|
|
77
82
|
// true
|
|
78
83
|
```
|
|
79
84
|
|
|
85
|
+
### `isBusinessDay(date: DateInput, options?: BusinessDayOptions): boolean`
|
|
86
|
+
|
|
87
|
+
Checks the input date itself in the selected time zone. Returns `false` for
|
|
88
|
+
Saturdays, Sundays, dates in `options.holidays`, and dates skipped entirely by
|
|
89
|
+
the time zone. A weekday that starts after midnight can still be a business day.
|
|
90
|
+
Invalid holiday inputs throw a `TypeError`, even when the input falls on a weekend.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const options = { timeZone: "Asia/Tokyo", holidays: ["2026-11-23"] }
|
|
94
|
+
|
|
95
|
+
isBusinessDay("2026-11-23", options)
|
|
96
|
+
// false
|
|
97
|
+
|
|
98
|
+
isBusinessDay("2026-11-24", options)
|
|
99
|
+
// true
|
|
100
|
+
```
|
|
101
|
+
|
|
80
102
|
### `nextBusinessDay(date: DateInput, options?: BusinessDayOptions): Date`
|
|
81
103
|
|
|
82
104
|
Returns a `Date` for the start of the next business day in the selected time
|
|
@@ -97,7 +119,7 @@ nextBusinessDay("2026-05-15", { holidays: ["2026-05-18"] })
|
|
|
97
119
|
|
|
98
120
|
## Time zones
|
|
99
121
|
|
|
100
|
-
All
|
|
122
|
+
All public functions accept an optional `timeZone` such as `"Asia/Tokyo"`.
|
|
101
123
|
When omitted, they keep the runtime-local behavior of 0.2. The runtime's
|
|
102
124
|
`Intl.DateTimeFormat` supplies the time zone rules. Unrecognized IANA names
|
|
103
125
|
throw a `RangeError`.
|
package/dist/index.d.ts
CHANGED
|
@@ -8,4 +8,5 @@ export interface BusinessDayOptions extends DateOptions {
|
|
|
8
8
|
export declare function formatJaDate(input: DateInput, options?: DateOptions): string;
|
|
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
|
+
export declare function isBusinessDay(input: DateInput, options?: BusinessDayOptions): boolean;
|
|
11
12
|
export declare function nextBusinessDay(input: DateInput, options?: BusinessDayOptions): Date;
|
package/dist/index.js
CHANGED
|
@@ -37,6 +37,12 @@ function checkedUtcDay(year, month, day) {
|
|
|
37
37
|
}
|
|
38
38
|
return value;
|
|
39
39
|
}
|
|
40
|
+
function dateOnlyDay(input) {
|
|
41
|
+
if (typeof input !== "string" || !DATE_ONLY.test(input))
|
|
42
|
+
return undefined;
|
|
43
|
+
const [year, month, day] = input.split("-").map(Number);
|
|
44
|
+
return checkedUtcDay(year, month, day);
|
|
45
|
+
}
|
|
40
46
|
function utcDateParts(day) {
|
|
41
47
|
const cycles = Math.floor(day / GREGORIAN_CYCLE_MS);
|
|
42
48
|
const date = new Date(day - cycles * GREGORIAN_CYCLE_MS);
|
|
@@ -79,10 +85,9 @@ function calendarDay(input, formatter) {
|
|
|
79
85
|
return utcDay(date.getFullYear(), date.getMonth() + 1, date.getDate());
|
|
80
86
|
}
|
|
81
87
|
if (typeof input === "string") {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return
|
|
85
|
-
}
|
|
88
|
+
const day = dateOnlyDay(input);
|
|
89
|
+
if (day !== undefined)
|
|
90
|
+
return day;
|
|
86
91
|
const match = OFFSET_DATE_TIME.exec(input);
|
|
87
92
|
if (!match)
|
|
88
93
|
throw new TypeError("Expected YYYY-MM-DD or an ISO date-time with an offset");
|
|
@@ -139,6 +144,20 @@ function isWeekend(day) {
|
|
|
139
144
|
const weekday = weekdayOf(day);
|
|
140
145
|
return weekday === 0 || weekday === 6;
|
|
141
146
|
}
|
|
147
|
+
function holidayDays(holidays, formatter) {
|
|
148
|
+
return new Set(holidays?.map((holiday) => calendarDay(holiday, formatter)));
|
|
149
|
+
}
|
|
150
|
+
function businessDayStart(candidate, holidays, formatter) {
|
|
151
|
+
if (isWeekend(candidate) || holidays.has(candidate))
|
|
152
|
+
return undefined;
|
|
153
|
+
if (formatter)
|
|
154
|
+
return zonedDayStart(candidate, formatter);
|
|
155
|
+
const { year, month, day } = utcDateParts(candidate);
|
|
156
|
+
const local = new Date(0);
|
|
157
|
+
local.setHours(0, 0, 0, 0);
|
|
158
|
+
local.setFullYear(year, month - 1, day);
|
|
159
|
+
return local.getDate() === day ? local : undefined;
|
|
160
|
+
}
|
|
142
161
|
export function formatJaDate(input, options = {}) {
|
|
143
162
|
const formatter = createFormatter(options.timeZone);
|
|
144
163
|
const date = calendarDay(input, formatter);
|
|
@@ -162,26 +181,20 @@ export function formatRelativeJaDate(input, baseInput = new Date(), options = {}
|
|
|
162
181
|
export function isWeekendJP(input, options = {}) {
|
|
163
182
|
return isWeekend(calendarDay(input, createFormatter(options.timeZone)));
|
|
164
183
|
}
|
|
184
|
+
export function isBusinessDay(input, options = {}) {
|
|
185
|
+
const formatter = createFormatter(options.timeZone);
|
|
186
|
+
const day = dateOnlyDay(input) ?? calendarDay(input, formatter);
|
|
187
|
+
const holidays = holidayDays(options.holidays, formatter);
|
|
188
|
+
return businessDayStart(day, holidays, formatter) !== undefined;
|
|
189
|
+
}
|
|
165
190
|
export function nextBusinessDay(input, options = {}) {
|
|
166
191
|
const formatter = createFormatter(options.timeZone);
|
|
167
192
|
const day = calendarDay(input, formatter);
|
|
168
|
-
const holidays =
|
|
193
|
+
const holidays = holidayDays(options.holidays, formatter);
|
|
169
194
|
for (let candidate = day + ONE_DAY_MS; candidate <= MAX_DATE_MS + ONE_DAY_MS; candidate += ONE_DAY_MS) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
const start = zonedDayStart(candidate, formatter);
|
|
174
|
-
if (start)
|
|
175
|
-
return start;
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
const { year, month, day } = utcDateParts(candidate);
|
|
179
|
-
const local = new Date(0);
|
|
180
|
-
local.setHours(0, 0, 0, 0);
|
|
181
|
-
local.setFullYear(year, month - 1, day);
|
|
182
|
-
if (local.getDate() === day)
|
|
183
|
-
return local;
|
|
184
|
-
}
|
|
195
|
+
const start = businessDayStart(candidate, holidays, formatter);
|
|
196
|
+
if (start)
|
|
197
|
+
return start;
|
|
185
198
|
}
|
|
186
199
|
throw new TypeError("Invalid date");
|
|
187
200
|
}
|