tiny-ja-date 0.2.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 +54 -9
- package/dist/index.d.ts +8 -4
- package/dist/index.js +165 -28
- package/package.json +2 -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,13 +34,16 @@ 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
|
```
|
|
38
43
|
|
|
39
44
|
## API
|
|
40
45
|
|
|
41
|
-
### `formatJaDate(date:
|
|
46
|
+
### `formatJaDate(date: DateInput, options?: DateOptions): string`
|
|
42
47
|
|
|
43
48
|
Formats a date as a Japanese date label.
|
|
44
49
|
|
|
@@ -47,7 +52,7 @@ formatJaDate(new Date(2026, 4, 12))
|
|
|
47
52
|
// "2026年5月12日(火)"
|
|
48
53
|
```
|
|
49
54
|
|
|
50
|
-
### `formatRelativeJaDate(date:
|
|
55
|
+
### `formatRelativeJaDate(date: DateInput, baseDate?: DateInput, options?: DateOptions): string`
|
|
51
56
|
|
|
52
57
|
Formats a date relative to a base date.
|
|
53
58
|
|
|
@@ -68,7 +73,7 @@ formatRelativeJaDate(new Date(2026, 4, 15), new Date(2026, 4, 12))
|
|
|
68
73
|
// "3日後"
|
|
69
74
|
```
|
|
70
75
|
|
|
71
|
-
### `isWeekendJP(date:
|
|
76
|
+
### `isWeekendJP(date: DateInput, options?: DateOptions): boolean`
|
|
72
77
|
|
|
73
78
|
Checks whether the date is Saturday or Sunday.
|
|
74
79
|
|
|
@@ -77,9 +82,28 @@ 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
|
-
Returns the start of the next business day in the
|
|
104
|
+
Returns a `Date` for the start of the next business day in the selected time
|
|
105
|
+
zone. If midnight is skipped, the result is the first valid time on that date.
|
|
106
|
+
If a time zone skipped an entire date, the search continues to the following date.
|
|
83
107
|
|
|
84
108
|
The search starts the day after the input and skips Saturdays, Sundays, and
|
|
85
109
|
any local calendar dates supplied in `options.holidays`. Each holiday accepts
|
|
@@ -93,15 +117,37 @@ nextBusinessDay("2026-05-15", { holidays: ["2026-05-18"] })
|
|
|
93
117
|
// Date for 2026-05-19
|
|
94
118
|
```
|
|
95
119
|
|
|
120
|
+
## Time zones
|
|
121
|
+
|
|
122
|
+
All public functions accept an optional `timeZone` such as `"Asia/Tokyo"`.
|
|
123
|
+
When omitted, they keep the runtime-local behavior of 0.2. The runtime's
|
|
124
|
+
`Intl.DateTimeFormat` supplies the time zone rules. Unrecognized IANA names
|
|
125
|
+
throw a `RangeError`.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const tokyo = { timeZone: "Asia/Tokyo" }
|
|
129
|
+
|
|
130
|
+
formatJaDate("2026-11-23T15:30:00Z", tokyo)
|
|
131
|
+
// "2026年11月24日(火)"
|
|
132
|
+
|
|
133
|
+
formatRelativeJaDate("2026-11-24", undefined, tokyo)
|
|
134
|
+
// Relative to today's date in Tokyo
|
|
135
|
+
|
|
136
|
+
nextBusinessDay("2026-11-20", { ...tokyo, holidays: ["2026-11-23"] }).toISOString()
|
|
137
|
+
// "2026-11-23T15:00:00.000Z" (2026-11-24 at 00:00 in Tokyo)
|
|
138
|
+
```
|
|
139
|
+
|
|
96
140
|
## Date inputs
|
|
97
141
|
|
|
98
|
-
- `YYYY-MM-DD` strings represent
|
|
142
|
+
- `YYYY-MM-DD` strings represent calendar dates as written in the selected time
|
|
143
|
+
zone. The runtime-local zone is used when no zone is specified.
|
|
99
144
|
Invalid dates such as `2026-02-30` throw a `TypeError`.
|
|
100
145
|
- `Date` objects and Unix timestamps in milliseconds represent instants. The
|
|
101
|
-
functions use the instant's date in the
|
|
146
|
+
functions use the instant's date in the selected time zone.
|
|
102
147
|
- For strings with a time, use ISO 8601 with an explicit offset such as
|
|
103
|
-
`2026-05-12T09:00:00+09:00` to identify an instant.
|
|
104
|
-
|
|
148
|
+
`2026-05-12T09:00:00+09:00` to identify an instant. With an explicit
|
|
149
|
+
`timeZone`, date-time strings without an offset and other string formats
|
|
150
|
+
throw a `TypeError`. Without `timeZone`, they follow JavaScript's `Date` parsing.
|
|
105
151
|
|
|
106
152
|
### Upgrading from 0.1
|
|
107
153
|
|
|
@@ -116,7 +162,6 @@ as `2026-05-12T00:00:00Z` to retain the 0.1 interpretation.
|
|
|
116
162
|
It does not:
|
|
117
163
|
|
|
118
164
|
- include Japanese holiday data
|
|
119
|
-
- convert time zones
|
|
120
165
|
- format wareki
|
|
121
166
|
- generate calendars
|
|
122
167
|
- replace date-fns or dayjs
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
export type DateInput = Date | string | number;
|
|
2
|
-
export interface
|
|
2
|
+
export interface DateOptions {
|
|
3
|
+
timeZone?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface BusinessDayOptions extends DateOptions {
|
|
3
6
|
holidays?: readonly DateInput[];
|
|
4
7
|
}
|
|
5
|
-
export declare function formatJaDate(input: DateInput): string;
|
|
6
|
-
export declare function formatRelativeJaDate(input: DateInput, baseInput?: DateInput): string;
|
|
7
|
-
export declare function isWeekendJP(input: DateInput): boolean;
|
|
8
|
+
export declare function formatJaDate(input: DateInput, options?: DateOptions): string;
|
|
9
|
+
export declare function formatRelativeJaDate(input: DateInput, baseInput?: DateInput, options?: DateOptions): string;
|
|
10
|
+
export declare function isWeekendJP(input: DateInput, options?: DateOptions): boolean;
|
|
11
|
+
export declare function isBusinessDay(input: DateInput, options?: BusinessDayOptions): boolean;
|
|
8
12
|
export declare function nextBusinessDay(input: DateInput, options?: BusinessDayOptions): Date;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
const WEEKDAYS_JA = ["日", "月", "火", "水", "木", "金", "土"];
|
|
2
2
|
const ONE_DAY_MS = 86400000;
|
|
3
|
+
const MAX_DATE_MS = 8640000000000000;
|
|
4
|
+
const GREGORIAN_CYCLE_MS = 146097 * ONE_DAY_MS;
|
|
5
|
+
const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
|
|
6
|
+
const OFFSET_DATE_TIME = /^((?:\d{4}|[+-]\d{6}))-(\d{2})-(\d{2})T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})$/i;
|
|
3
7
|
function toValidDate(input) {
|
|
4
|
-
if (typeof input === "string" &&
|
|
8
|
+
if (typeof input === "string" && DATE_ONLY.test(input)) {
|
|
5
9
|
const [year, month, day] = input.split("-").map(Number);
|
|
6
10
|
const date = new Date(0);
|
|
7
11
|
date.setHours(0, 0, 0, 0);
|
|
@@ -19,23 +23,151 @@ function toValidDate(input) {
|
|
|
19
23
|
}
|
|
20
24
|
return date;
|
|
21
25
|
}
|
|
22
|
-
function
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
function utcDay(year, month, day) {
|
|
27
|
+
const date = new Date(0);
|
|
28
|
+
// A zone can project an instant near the Date limit into an adjacent day.
|
|
29
|
+
const cycles = Math.floor((year - 2000) / 400);
|
|
30
|
+
return date.setUTCFullYear(year - cycles * 400, month - 1, day) + cycles * GREGORIAN_CYCLE_MS;
|
|
31
|
+
}
|
|
32
|
+
function checkedUtcDay(year, month, day) {
|
|
33
|
+
const value = utcDay(year, month, day);
|
|
34
|
+
const date = utcDateParts(value);
|
|
35
|
+
if (date.year !== year || date.month !== month || date.day !== day) {
|
|
36
|
+
throw new TypeError("Invalid date");
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
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
|
+
}
|
|
46
|
+
function utcDateParts(day) {
|
|
47
|
+
const cycles = Math.floor(day / GREGORIAN_CYCLE_MS);
|
|
48
|
+
const date = new Date(day - cycles * GREGORIAN_CYCLE_MS);
|
|
49
|
+
return {
|
|
50
|
+
year: date.getUTCFullYear() + cycles * 400,
|
|
51
|
+
month: date.getUTCMonth() + 1,
|
|
52
|
+
day: date.getUTCDate(),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function createFormatter(timeZone) {
|
|
56
|
+
if (timeZone === undefined)
|
|
57
|
+
return undefined;
|
|
58
|
+
if (/^[+-]/.test(timeZone))
|
|
59
|
+
throw new RangeError("Expected an IANA time zone name");
|
|
60
|
+
return new Intl.DateTimeFormat("en-US", {
|
|
61
|
+
timeZone,
|
|
62
|
+
calendar: "gregory",
|
|
63
|
+
numberingSystem: "latn",
|
|
64
|
+
era: "short",
|
|
65
|
+
year: "numeric",
|
|
66
|
+
month: "numeric",
|
|
67
|
+
day: "numeric",
|
|
68
|
+
hour: "numeric",
|
|
69
|
+
minute: "numeric",
|
|
70
|
+
second: "numeric",
|
|
71
|
+
hourCycle: "h23",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
// Express the zoned wall clock on the UTC timeline for offset and day arithmetic.
|
|
75
|
+
function wallTime(instant, formatter) {
|
|
76
|
+
const parts = Object.fromEntries(formatter.formatToParts(new Date(instant))
|
|
77
|
+
.map(({ type, value }) => [type, value]));
|
|
78
|
+
const year = Number(parts.year);
|
|
79
|
+
const day = utcDay(parts.era === "BC" ? 1 - year : year, Number(parts.month), Number(parts.day));
|
|
80
|
+
return day + (Number(parts.hour) * 3600 + Number(parts.minute) * 60 + Number(parts.second)) * 1000;
|
|
81
|
+
}
|
|
82
|
+
function calendarDay(input, formatter) {
|
|
83
|
+
if (!formatter) {
|
|
84
|
+
const date = toValidDate(input);
|
|
85
|
+
return utcDay(date.getFullYear(), date.getMonth() + 1, date.getDate());
|
|
86
|
+
}
|
|
87
|
+
if (typeof input === "string") {
|
|
88
|
+
const day = dateOnlyDay(input);
|
|
89
|
+
if (day !== undefined)
|
|
90
|
+
return day;
|
|
91
|
+
const match = OFFSET_DATE_TIME.exec(input);
|
|
92
|
+
if (!match)
|
|
93
|
+
throw new TypeError("Expected YYYY-MM-DD or an ISO date-time with an offset");
|
|
94
|
+
checkedUtcDay(Number(match[1]), Number(match[2]), Number(match[3]));
|
|
95
|
+
}
|
|
96
|
+
const time = wallTime(toValidDate(input).getTime(), formatter);
|
|
97
|
+
return Math.floor(time / ONE_DAY_MS) * ONE_DAY_MS;
|
|
98
|
+
}
|
|
99
|
+
function zonedDayStart(day, formatter) {
|
|
100
|
+
const first = Math.max(-MAX_DATE_MS, day - ONE_DAY_MS * 2);
|
|
101
|
+
const last = Math.min(MAX_DATE_MS, day + ONE_DAY_MS * 2);
|
|
102
|
+
const offsets = new Set();
|
|
103
|
+
let earliest = Infinity;
|
|
104
|
+
let previous = first;
|
|
105
|
+
let previousWall = wallTime(first, formatter);
|
|
106
|
+
if (previousWall >= day && previousWall < day + ONE_DAY_MS)
|
|
107
|
+
earliest = first;
|
|
108
|
+
// Inspect offsets on both sides of midnight to handle repeated wall times.
|
|
109
|
+
for (let instant = first;; instant = Math.min(instant + ONE_DAY_MS / 4, last)) {
|
|
110
|
+
const wall = wallTime(instant, formatter);
|
|
111
|
+
offsets.add(wall - instant);
|
|
112
|
+
// A forward jump can begin the date after midnight or skip the whole date.
|
|
113
|
+
if (previousWall < day && wall >= day) {
|
|
114
|
+
let before = previous;
|
|
115
|
+
let after = instant;
|
|
116
|
+
while (after - before > 1000) {
|
|
117
|
+
const middle = Math.floor((before + after) / 2000) * 1000;
|
|
118
|
+
if (wallTime(middle, formatter) < day)
|
|
119
|
+
before = middle;
|
|
120
|
+
else
|
|
121
|
+
after = middle;
|
|
122
|
+
}
|
|
123
|
+
if (wallTime(after, formatter) < day + ONE_DAY_MS)
|
|
124
|
+
earliest = Math.min(earliest, after);
|
|
125
|
+
}
|
|
126
|
+
if (instant === last)
|
|
127
|
+
break;
|
|
128
|
+
previous = instant;
|
|
129
|
+
previousWall = wall;
|
|
130
|
+
}
|
|
131
|
+
for (const offset of offsets) {
|
|
132
|
+
const instant = day - offset;
|
|
133
|
+
if (Math.abs(instant) <= MAX_DATE_MS && wallTime(instant, formatter) === day) {
|
|
134
|
+
earliest = Math.min(earliest, instant);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return Number.isFinite(earliest) ? new Date(earliest) : undefined;
|
|
138
|
+
}
|
|
139
|
+
function weekdayOf(day) {
|
|
140
|
+
const weekday = (day / ONE_DAY_MS + 4) % 7;
|
|
141
|
+
return (weekday + 7) % 7;
|
|
142
|
+
}
|
|
143
|
+
function isWeekend(day) {
|
|
144
|
+
const weekday = weekdayOf(day);
|
|
145
|
+
return weekday === 0 || weekday === 6;
|
|
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
|
+
}
|
|
161
|
+
export function formatJaDate(input, options = {}) {
|
|
162
|
+
const formatter = createFormatter(options.timeZone);
|
|
163
|
+
const date = calendarDay(input, formatter);
|
|
164
|
+
const { year, month, day } = utcDateParts(date);
|
|
165
|
+
const weekday = WEEKDAYS_JA[weekdayOf(date)];
|
|
33
166
|
return `${year}年${month}月${day}日(${weekday})`;
|
|
34
167
|
}
|
|
35
|
-
export function formatRelativeJaDate(input, baseInput = new Date()) {
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
const diffDays = localDayNumber(date) - localDayNumber(baseDate);
|
|
168
|
+
export function formatRelativeJaDate(input, baseInput = new Date(), options = {}) {
|
|
169
|
+
const formatter = createFormatter(options.timeZone);
|
|
170
|
+
const diffDays = (calendarDay(input, formatter) - calendarDay(baseInput, formatter)) / ONE_DAY_MS;
|
|
39
171
|
if (diffDays === 0)
|
|
40
172
|
return "今日";
|
|
41
173
|
if (diffDays === -1)
|
|
@@ -46,18 +178,23 @@ export function formatRelativeJaDate(input, baseInput = new Date()) {
|
|
|
46
178
|
return `${Math.abs(diffDays)}日前`;
|
|
47
179
|
return `${diffDays}日後`;
|
|
48
180
|
}
|
|
49
|
-
export function isWeekendJP(input) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
181
|
+
export function isWeekendJP(input, options = {}) {
|
|
182
|
+
return isWeekend(calendarDay(input, createFormatter(options.timeZone)));
|
|
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;
|
|
53
189
|
}
|
|
54
190
|
export function nextBusinessDay(input, options = {}) {
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
191
|
+
const formatter = createFormatter(options.timeZone);
|
|
192
|
+
const day = calendarDay(input, formatter);
|
|
193
|
+
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) {
|
|
195
|
+
const start = businessDayStart(candidate, holidays, formatter);
|
|
196
|
+
if (start)
|
|
197
|
+
return start;
|
|
198
|
+
}
|
|
199
|
+
throw new TypeError("Invalid date");
|
|
63
200
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-ja-date",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Tiny TypeScript date utilities for Japanese UI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"datetime",
|
|
27
27
|
"format",
|
|
28
28
|
"relative-date",
|
|
29
|
+
"time-zone",
|
|
29
30
|
"business-day",
|
|
30
31
|
"typescript"
|
|
31
32
|
],
|