tiny-ja-date 0.1.0 → 0.2.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 +24 -6
- package/dist/index.d.ts +4 -1
- package/dist/index.js +24 -11
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -77,22 +77,37 @@ 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 the start of the next business day in the runtime's local time zone.
|
|
83
83
|
|
|
84
|
-
The input
|
|
84
|
+
The search starts the day after the input and skips Saturdays, Sundays, and
|
|
85
|
+
any local calendar dates supplied in `options.holidays`. Each holiday accepts
|
|
86
|
+
the same `DateInput` type as the input date.
|
|
85
87
|
|
|
86
88
|
```ts
|
|
87
89
|
nextBusinessDay(new Date(2026, 4, 15))
|
|
88
90
|
// Date for 2026-05-18
|
|
91
|
+
|
|
92
|
+
nextBusinessDay("2026-05-15", { holidays: ["2026-05-18"] })
|
|
93
|
+
// Date for 2026-05-19
|
|
89
94
|
```
|
|
90
95
|
|
|
91
|
-
##
|
|
96
|
+
## Date inputs
|
|
97
|
+
|
|
98
|
+
- `YYYY-MM-DD` strings represent local calendar dates in the runtime's time zone.
|
|
99
|
+
Invalid dates such as `2026-02-30` throw a `TypeError`.
|
|
100
|
+
- `Date` objects and Unix timestamps in milliseconds represent instants. The
|
|
101
|
+
functions use the instant's date in the runtime's local time zone.
|
|
102
|
+
- 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. Strings without an
|
|
104
|
+
explicit offset and other string formats follow JavaScript's `Date` parsing.
|
|
92
105
|
|
|
93
|
-
|
|
106
|
+
### Upgrading from 0.1
|
|
94
107
|
|
|
95
|
-
|
|
108
|
+
In 0.1, JavaScript parsed a bare `YYYY-MM-DD` string as midnight UTC. In 0.2,
|
|
109
|
+
it represents the local date as written. Pass an explicit UTC timestamp such
|
|
110
|
+
as `2026-05-12T00:00:00Z` to retain the 0.1 interpretation.
|
|
96
111
|
|
|
97
112
|
## Design policy
|
|
98
113
|
|
|
@@ -110,6 +125,9 @@ It does not:
|
|
|
110
125
|
|
|
111
126
|
## Live Demo
|
|
112
127
|
|
|
128
|
+
Run `npm run build:demo` after changing the library source to refresh
|
|
129
|
+
`docs/index.js`, the compiled module used by the demo.
|
|
130
|
+
|
|
113
131
|
- Demo → `https://fusaihanabi.github.io/tiny-ja-date`
|
|
114
132
|
- npm → `https://www.npmjs.com/package/tiny-ja-date`
|
|
115
133
|
- GitHub → `https://github.com/fusaihanabi/tiny-ja-date`
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export type DateInput = Date | string | number;
|
|
2
|
+
export interface BusinessDayOptions {
|
|
3
|
+
holidays?: readonly DateInput[];
|
|
4
|
+
}
|
|
2
5
|
export declare function formatJaDate(input: DateInput): string;
|
|
3
6
|
export declare function formatRelativeJaDate(input: DateInput, baseInput?: DateInput): string;
|
|
4
7
|
export declare function isWeekendJP(input: DateInput): boolean;
|
|
5
|
-
export declare function nextBusinessDay(input: DateInput): Date;
|
|
8
|
+
export declare function nextBusinessDay(input: DateInput, options?: BusinessDayOptions): Date;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
const WEEKDAYS_JA = ["日", "月", "火", "水", "木", "金", "土"];
|
|
2
2
|
const ONE_DAY_MS = 86400000;
|
|
3
3
|
function toValidDate(input) {
|
|
4
|
+
if (typeof input === "string" && /^\d{4}-\d{2}-\d{2}$/.test(input)) {
|
|
5
|
+
const [year, month, day] = input.split("-").map(Number);
|
|
6
|
+
const date = new Date(0);
|
|
7
|
+
date.setHours(0, 0, 0, 0);
|
|
8
|
+
date.setFullYear(year, month - 1, day);
|
|
9
|
+
if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) {
|
|
10
|
+
throw new TypeError("Invalid date");
|
|
11
|
+
}
|
|
12
|
+
return date;
|
|
13
|
+
}
|
|
4
14
|
const date = input instanceof Date
|
|
5
15
|
? new Date(input.getTime())
|
|
6
16
|
: new Date(input);
|
|
@@ -9,8 +19,10 @@ function toValidDate(input) {
|
|
|
9
19
|
}
|
|
10
20
|
return date;
|
|
11
21
|
}
|
|
12
|
-
function
|
|
13
|
-
|
|
22
|
+
function localDayNumber(date) {
|
|
23
|
+
const utcDay = new Date(0);
|
|
24
|
+
utcDay.setUTCFullYear(date.getFullYear(), date.getMonth(), date.getDate());
|
|
25
|
+
return utcDay.getTime() / ONE_DAY_MS;
|
|
14
26
|
}
|
|
15
27
|
export function formatJaDate(input) {
|
|
16
28
|
const date = toValidDate(input);
|
|
@@ -21,10 +33,9 @@ export function formatJaDate(input) {
|
|
|
21
33
|
return `${year}年${month}月${day}日(${weekday})`;
|
|
22
34
|
}
|
|
23
35
|
export function formatRelativeJaDate(input, baseInput = new Date()) {
|
|
24
|
-
const date =
|
|
25
|
-
const baseDate =
|
|
26
|
-
const
|
|
27
|
-
const diffDays = Math.round(diffMs / ONE_DAY_MS);
|
|
36
|
+
const date = toValidDate(input);
|
|
37
|
+
const baseDate = toValidDate(baseInput);
|
|
38
|
+
const diffDays = localDayNumber(date) - localDayNumber(baseDate);
|
|
28
39
|
if (diffDays === 0)
|
|
29
40
|
return "今日";
|
|
30
41
|
if (diffDays === -1)
|
|
@@ -40,11 +51,13 @@ export function isWeekendJP(input) {
|
|
|
40
51
|
const day = date.getDay();
|
|
41
52
|
return day === 0 || day === 6;
|
|
42
53
|
}
|
|
43
|
-
export function nextBusinessDay(input) {
|
|
44
|
-
const date =
|
|
45
|
-
|
|
46
|
-
|
|
54
|
+
export function nextBusinessDay(input, options = {}) {
|
|
55
|
+
const date = toValidDate(input);
|
|
56
|
+
const holidays = new Set(options.holidays?.map((holiday) => localDayNumber(toValidDate(holiday))));
|
|
57
|
+
date.setHours(0, 0, 0, 0);
|
|
58
|
+
do {
|
|
47
59
|
date.setDate(date.getDate() + 1);
|
|
48
|
-
|
|
60
|
+
date.setHours(0, 0, 0, 0);
|
|
61
|
+
} while (isWeekendJP(date) || holidays.has(localDayNumber(date)));
|
|
49
62
|
return date;
|
|
50
63
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-ja-date",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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",
|