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