@kkkf/utils 0.0.1 → 0.0.2

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/dist/index.d.mts CHANGED
@@ -7,4 +7,15 @@ declare function multiply(a: number, b: number): number;
7
7
  /** 精确除法 */
8
8
  declare function divide(a: number, b: number): number;
9
9
 
10
- export { add, divide, multiply, subtract };
10
+ /**
11
+ * 计算两个日期相隔的天数,返回中文描述
12
+ *
13
+ * @example
14
+ * dateDiff('2024-01-01', '2024-01-02') → '1天'
15
+ * dateDiff('2024/01/01', '2024.02.02') → '1个月零1天'
16
+ * dateDiff('20240101', '2025-03-05') → '1年零2个月零4天'
17
+ * dateDiff('2024-01-01', '2024-01-01') → '0天'
18
+ */
19
+ declare function dateDiff(start: string | number | Date, end: string | number | Date): string;
20
+
21
+ export { add, dateDiff, divide, multiply, subtract };
package/dist/index.d.ts CHANGED
@@ -7,4 +7,15 @@ declare function multiply(a: number, b: number): number;
7
7
  /** 精确除法 */
8
8
  declare function divide(a: number, b: number): number;
9
9
 
10
- export { add, divide, multiply, subtract };
10
+ /**
11
+ * 计算两个日期相隔的天数,返回中文描述
12
+ *
13
+ * @example
14
+ * dateDiff('2024-01-01', '2024-01-02') → '1天'
15
+ * dateDiff('2024/01/01', '2024.02.02') → '1个月零1天'
16
+ * dateDiff('20240101', '2025-03-05') → '1年零2个月零4天'
17
+ * dateDiff('2024-01-01', '2024-01-01') → '0天'
18
+ */
19
+ declare function dateDiff(start: string | number | Date, end: string | number | Date): string;
20
+
21
+ export { add, dateDiff, divide, multiply, subtract };
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  add: () => add,
24
+ dateDiff: () => dateDiff,
24
25
  divide: () => divide,
25
26
  multiply: () => multiply,
26
27
  subtract: () => subtract
@@ -58,9 +59,45 @@ function divide(a, b) {
58
59
  const intB = Number(String(b).replace(".", ""));
59
60
  return intA / intB * Math.pow(10, lenB - lenA);
60
61
  }
62
+
63
+ // src/date.ts
64
+ function parseDate(input) {
65
+ if (input instanceof Date) return input;
66
+ if (typeof input === "number") return new Date(input);
67
+ const str = input.trim();
68
+ if (/^\d{8}$/.test(str)) {
69
+ const y = str.slice(0, 4);
70
+ const m = str.slice(4, 6);
71
+ const d = str.slice(6, 8);
72
+ return /* @__PURE__ */ new Date(`${y}-${m}-${d}`);
73
+ }
74
+ const normalized = str.replace(/[/.]/g, "-");
75
+ const date = new Date(normalized);
76
+ if (isNaN(date.getTime())) {
77
+ throw new Error(`\u65E0\u6CD5\u89E3\u6790\u65E5\u671F: ${input}`);
78
+ }
79
+ return date;
80
+ }
81
+ function dateDiff(start, end) {
82
+ const startDate = parseDate(start);
83
+ const endDate = parseDate(end);
84
+ const [earlier, later] = startDate <= endDate ? [startDate, endDate] : [endDate, startDate];
85
+ const diffTime = later.getTime() - earlier.getTime();
86
+ const totalDays = Math.floor(diffTime / (1e3 * 60 * 60 * 24));
87
+ if (totalDays === 0) return "0\u5929";
88
+ const years = Math.floor(totalDays / 365);
89
+ const months = Math.floor(totalDays % 365 / 30);
90
+ const days = totalDays % 365 % 30;
91
+ const parts = [];
92
+ if (years > 0) parts.push(`${years}\u5E74`);
93
+ if (months > 0) parts.push(`${months}\u4E2A\u6708`);
94
+ if (days > 0) parts.push(`${days}\u5929`);
95
+ return parts.join("\u96F6");
96
+ }
61
97
  // Annotate the CommonJS export names for ESM import in node:
62
98
  0 && (module.exports = {
63
99
  add,
100
+ dateDiff,
64
101
  divide,
65
102
  multiply,
66
103
  subtract
package/dist/index.mjs CHANGED
@@ -29,8 +29,44 @@ function divide(a, b) {
29
29
  const intB = Number(String(b).replace(".", ""));
30
30
  return intA / intB * Math.pow(10, lenB - lenA);
31
31
  }
32
+
33
+ // src/date.ts
34
+ function parseDate(input) {
35
+ if (input instanceof Date) return input;
36
+ if (typeof input === "number") return new Date(input);
37
+ const str = input.trim();
38
+ if (/^\d{8}$/.test(str)) {
39
+ const y = str.slice(0, 4);
40
+ const m = str.slice(4, 6);
41
+ const d = str.slice(6, 8);
42
+ return /* @__PURE__ */ new Date(`${y}-${m}-${d}`);
43
+ }
44
+ const normalized = str.replace(/[/.]/g, "-");
45
+ const date = new Date(normalized);
46
+ if (isNaN(date.getTime())) {
47
+ throw new Error(`\u65E0\u6CD5\u89E3\u6790\u65E5\u671F: ${input}`);
48
+ }
49
+ return date;
50
+ }
51
+ function dateDiff(start, end) {
52
+ const startDate = parseDate(start);
53
+ const endDate = parseDate(end);
54
+ const [earlier, later] = startDate <= endDate ? [startDate, endDate] : [endDate, startDate];
55
+ const diffTime = later.getTime() - earlier.getTime();
56
+ const totalDays = Math.floor(diffTime / (1e3 * 60 * 60 * 24));
57
+ if (totalDays === 0) return "0\u5929";
58
+ const years = Math.floor(totalDays / 365);
59
+ const months = Math.floor(totalDays % 365 / 30);
60
+ const days = totalDays % 365 % 30;
61
+ const parts = [];
62
+ if (years > 0) parts.push(`${years}\u5E74`);
63
+ if (months > 0) parts.push(`${months}\u4E2A\u6708`);
64
+ if (days > 0) parts.push(`${days}\u5929`);
65
+ return parts.join("\u96F6");
66
+ }
32
67
  export {
33
68
  add,
69
+ dateDiff,
34
70
  divide,
35
71
  multiply,
36
72
  subtract
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kkkf/utils",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
package/src/date.ts ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * 解析任意格式的日期字符串为 Date 对象
3
+ * 支持:2024-01-01、2024/01/01、2024.01.01、20240101、Date对象、时间戳
4
+ */
5
+ function parseDate(input: string | number | Date): Date {
6
+ if (input instanceof Date) return input
7
+ if (typeof input === 'number') return new Date(input)
8
+
9
+ // 去掉首尾空格
10
+ const str = input.trim()
11
+
12
+ // 处理纯数字:20240101
13
+ if (/^\d{8}$/.test(str)) {
14
+ const y = str.slice(0, 4)
15
+ const m = str.slice(4, 6)
16
+ const d = str.slice(6, 8)
17
+ return new Date(`${y}-${m}-${d}`)
18
+ }
19
+
20
+ // 统一把 / . 替换成 -
21
+ const normalized = str.replace(/[/.]/g, '-')
22
+ const date = new Date(normalized)
23
+
24
+ if (isNaN(date.getTime())) {
25
+ throw new Error(`无法解析日期: ${input}`)
26
+ }
27
+
28
+ return date
29
+ }
30
+
31
+ /**
32
+ * 计算两个日期相隔的天数,返回中文描述
33
+ *
34
+ * @example
35
+ * dateDiff('2024-01-01', '2024-01-02') → '1天'
36
+ * dateDiff('2024/01/01', '2024.02.02') → '1个月零1天'
37
+ * dateDiff('20240101', '2025-03-05') → '1年零2个月零4天'
38
+ * dateDiff('2024-01-01', '2024-01-01') → '0天'
39
+ */
40
+ export function dateDiff(start: string | number | Date, end: string | number | Date): string {
41
+ const startDate = parseDate(start)
42
+ const endDate = parseDate(end)
43
+
44
+ // 保证 start <= end
45
+ const [earlier, later] = startDate <= endDate
46
+ ? [startDate, endDate]
47
+ : [endDate, startDate]
48
+
49
+ // 计算总天数
50
+ const diffTime = later.getTime() - earlier.getTime()
51
+ const totalDays = Math.floor(diffTime / (1000 * 60 * 60 * 24))
52
+
53
+ if (totalDays === 0) return '0天'
54
+
55
+ const years = Math.floor(totalDays / 365)
56
+ const months = Math.floor((totalDays % 365) / 30)
57
+ const days = totalDays % 365 % 30
58
+
59
+ const parts: string[] = []
60
+ if (years > 0) parts.push(`${years}年`)
61
+ if (months > 0) parts.push(`${months}个月`)
62
+ if (days > 0) parts.push(`${days}天`)
63
+
64
+ return parts.join('零')
65
+ }
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
- export { add, subtract, multiply, divide } from './math'
1
+ export { add, subtract, multiply, divide } from './math'
2
+ export { dateDiff } from './date'