@boo-dreamer/date-utils 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.cjs ADDED
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function formatDate(date, format = "YYYY-MM-DD") {
4
+ const d = normalizeDate$1(date);
5
+ if (isNaN(d.getTime())) {
6
+ console.warn("[DateUtils] Invalid date input:", date);
7
+ return "";
8
+ }
9
+ const tokens = {
10
+ YYYY: d.getFullYear(),
11
+ MM: padZero(d.getMonth() + 1),
12
+ DD: padZero(d.getDate()),
13
+ HH: padZero(d.getHours()),
14
+ mm: padZero(d.getMinutes()),
15
+ ss: padZero(d.getSeconds())
16
+ };
17
+ return format.replace(/YYYY|MM|DD|HH|mm|ss/g, (match) => String(tokens[match]));
18
+ }
19
+ function normalizeDate$1(date) {
20
+ if (date instanceof Date) {
21
+ return date;
22
+ }
23
+ if (typeof date === "number") {
24
+ return new Date(date);
25
+ }
26
+ const parsed = new Date(date);
27
+ return parsed;
28
+ }
29
+ function padZero(num) {
30
+ return num < 10 ? `0${num}` : String(num);
31
+ }
32
+ function getNow(format = "YYYY-MM-DD HH:mm:ss") {
33
+ return formatDate(/* @__PURE__ */ new Date(), format);
34
+ }
35
+ function getToday(format = "YYYY-MM-DD") {
36
+ return formatDate(/* @__PURE__ */ new Date(), format);
37
+ }
38
+ function addDays(date, days) {
39
+ const d = normalizeDate(date);
40
+ const result = new Date(d);
41
+ result.setDate(result.getDate() + days);
42
+ return result;
43
+ }
44
+ function addMonths(date, months) {
45
+ const d = normalizeDate(date);
46
+ const result = new Date(d);
47
+ result.setMonth(result.getMonth() + months);
48
+ return result;
49
+ }
50
+ function addYears(date, years) {
51
+ const d = normalizeDate(date);
52
+ const result = new Date(d);
53
+ result.setFullYear(result.getFullYear() + years);
54
+ return result;
55
+ }
56
+ function add(date, amount, unit) {
57
+ const normalizedUnit = unit.toLowerCase();
58
+ switch (normalizedUnit) {
59
+ case "day":
60
+ case "days":
61
+ return addDays(date, amount);
62
+ case "week":
63
+ case "weeks":
64
+ return addDays(date, amount * 7);
65
+ case "month":
66
+ case "months":
67
+ return addMonths(date, amount);
68
+ case "year":
69
+ case "years":
70
+ return addYears(date, amount);
71
+ default:
72
+ throw new Error(`[DateUtils] Unsupported time unit: ${unit}`);
73
+ }
74
+ }
75
+ function diffDays(date1, date2) {
76
+ const d1 = normalizeDate(date1);
77
+ const d2 = normalizeDate(date2);
78
+ const utc1 = Date.UTC(d1.getFullYear(), d1.getMonth(), d1.getDate());
79
+ const utc2 = Date.UTC(d2.getFullYear(), d2.getMonth(), d2.getDate());
80
+ const msPerDay = 1e3 * 60 * 60 * 24;
81
+ return Math.floor((utc2 - utc1) / msPerDay);
82
+ }
83
+ function isSameDay(date1, date2) {
84
+ return diffDays(date1, date2) === 0;
85
+ }
86
+ function isToday(date) {
87
+ return isSameDay(date, /* @__PURE__ */ new Date());
88
+ }
89
+ function normalizeDate(date) {
90
+ if (date instanceof Date) {
91
+ return new Date(date);
92
+ }
93
+ if (typeof date === "number") {
94
+ return new Date(date);
95
+ }
96
+ return new Date(date);
97
+ }
98
+ const version = "__VERSION__";
99
+ exports.add = add;
100
+ exports.addDays = addDays;
101
+ exports.addMonths = addMonths;
102
+ exports.addYears = addYears;
103
+ exports.diffDays = diffDays;
104
+ exports.formatDate = formatDate;
105
+ exports.getNow = getNow;
106
+ exports.getToday = getToday;
107
+ exports.isSameDay = isSameDay;
108
+ exports.isToday = isToday;
109
+ exports.version = version;
110
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/format.ts","../src/calculate.ts","../src/index.ts"],"sourcesContent":["/**\n * 日期格式化模块\n */\n\nexport type DateFormat =\n | 'YYYY-MM-DD'\n | 'YYYY/MM/DD'\n | 'YYYY-MM-DD HH:mm:ss'\n | 'YYYY/MM/DD HH:mm:ss'\n | 'HH:mm:ss'\n | 'HH:mm'\n | string;\n\n/**\n * 将日期格式化为指定格式的字符串\n * @param date - 要格式化的日期(Date 对象、时间戳或日期字符串)\n * @param format - 格式化模板,默认为 'YYYY-MM-DD'\n * @returns 格式化后的日期字符串\n * @example\n * ```ts\n * formatDate(new Date(), 'YYYY-MM-DD') // '2024-01-15'\n * formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss') // '2024-01-15 14:30:00'\n * ```\n */\nexport function formatDate(\n date: Date | number | string,\n format: DateFormat = 'YYYY-MM-DD'\n): string {\n const d = normalizeDate(date);\n\n if (isNaN(d.getTime())) {\n console.warn('[DateUtils] Invalid date input:', date);\n return '';\n }\n\n const tokens: Record<string, string | number> = {\n YYYY: d.getFullYear(),\n MM: padZero(d.getMonth() + 1),\n DD: padZero(d.getDate()),\n HH: padZero(d.getHours()),\n mm: padZero(d.getMinutes()),\n ss: padZero(d.getSeconds()),\n };\n\n return format.replace(/YYYY|MM|DD|HH|mm|ss/g, match => String(tokens[match]));\n}\n\n/**\n * 标准化日期输入为 Date 对象\n */\nfunction normalizeDate(date: Date | number | string): Date {\n if (date instanceof Date) {\n return date;\n }\n if (typeof date === 'number') {\n return new Date(date);\n }\n // 尝试解析字符串\n const parsed = new Date(date);\n return parsed;\n}\n\n/**\n * 数字补零\n */\nfunction padZero(num: number): string {\n return num < 10 ? `0${num}` : String(num);\n}\n\n/**\n * 获取当前日期的格式化字符串\n * @param format - 格式化模板\n * @returns 格式化后的当前日期字符串\n */\nexport function getNow(format: DateFormat = 'YYYY-MM-DD HH:mm:ss'): string {\n return formatDate(new Date(), format);\n}\n\n/**\n * 获取今天的日期字符串\n * @param format - 格式化模板,默认为 'YYYY-MM-DD'\n * @returns 今天的日期字符串\n */\nexport function getToday(format: DateFormat = 'YYYY-MM-DD'): string {\n return formatDate(new Date(), format);\n}\n","/**\n * 日期计算模块\n */\n\nexport type TimeUnit = 'day' | 'days' | 'week' | 'weeks' | 'month' | 'months' | 'year' | 'years';\n\n/**\n * 在指定日期上添加天数\n * @param date - 基准日期\n * @param days - 要添加的天数(可为负数)\n * @returns 新的 Date 对象\n * @example\n * ```ts\n * addDays(new Date('2024-01-15'), 5) // Date('2024-01-20')\n * addDays(new Date('2024-01-15'), -3) // Date('2024-01-12')\n * ```\n */\nexport function addDays(date: Date | number | string, days: number): Date {\n const d = normalizeDate(date);\n const result = new Date(d);\n result.setDate(result.getDate() + days);\n return result;\n}\n\n/**\n * 在指定日期上添加月份\n * @param date - 基准日期\n * @param months - 要添加的月数(可为负数)\n * @returns 新的 Date 对象\n */\nexport function addMonths(date: Date | number | string, months: number): Date {\n const d = normalizeDate(date);\n const result = new Date(d);\n result.setMonth(result.getMonth() + months);\n return result;\n}\n\n/**\n * 在指定日期上添加年份\n * @param date - 基准日期\n * @param years - 要添加的年数(可为负数)\n * @returns 新的 Date 对象\n */\nexport function addYears(date: Date | number | string, years: number): Date {\n const d = normalizeDate(date);\n const result = new Date(d);\n result.setFullYear(result.getFullYear() + years);\n return result;\n}\n\n/**\n * 通用添加时间方法\n * @param date - 基准日期\n * @param amount - 数量\n * @param unit - 时间单位\n * @returns 新的 Date 对象\n * @example\n * ```ts\n * add(new Date(), 1, 'week') // 一周后\n * add(new Date(), -2, 'month') // 两个月前\n * ```\n */\nexport function add(date: Date | number | string, amount: number, unit: TimeUnit): Date {\n const normalizedUnit = unit.toLowerCase() as TimeUnit;\n\n switch (normalizedUnit) {\n case 'day':\n case 'days':\n return addDays(date, amount);\n case 'week':\n case 'weeks':\n return addDays(date, amount * 7);\n case 'month':\n case 'months':\n return addMonths(date, amount);\n case 'year':\n case 'years':\n return addYears(date, amount);\n default:\n throw new Error(`[DateUtils] Unsupported time unit: ${unit}`);\n }\n}\n\n/**\n * 计算两个日期之间的天数差\n * @param date1 - 第一个日期\n * @param date2 - 第二个日期\n * @returns 天数差(date2 - date1)\n */\nexport function diffDays(\n date1: Date | number | string,\n date2: Date | number | string\n): number {\n const d1 = normalizeDate(date1);\n const d2 = normalizeDate(date2);\n\n // 重置时间为当天零点,避免时区问题\n const utc1 = Date.UTC(d1.getFullYear(), d1.getMonth(), d1.getDate());\n const utc2 = Date.UTC(d2.getFullYear(), d2.getMonth(), d2.getDate());\n\n const msPerDay = 1000 * 60 * 60 * 24;\n return Math.floor((utc2 - utc1) / msPerDay);\n}\n\n/**\n * 判断是否为同一天\n * @param date1 - 第一个日期\n * @param date2 - 第二个日期\n * @returns 是否为同一天\n */\nexport function isSameDay(\n date1: Date | number | string,\n date2: Date | number | string\n): boolean {\n return diffDays(date1, date2) === 0;\n}\n\n/**\n * 判断日期是否为今天\n * @param date - 要判断的日期\n * @returns 是否为今天\n */\nexport function isToday(date: Date | number | string): boolean {\n return isSameDay(date, new Date());\n}\n\n/**\n * 标准化日期输入为 Date 对象\n */\nfunction normalizeDate(date: Date | number | string): Date {\n if (date instanceof Date) {\n return new Date(date);\n }\n if (typeof date === 'number') {\n return new Date(date);\n }\n return new Date(date);\n}\n","/**\n * @boo-dreamer/date-utils\n * 日期工具函数库\n */\n\nexport * from './format';\nexport * from './calculate';\n\n// 版本信息\nexport const version = '__VERSION__';\n"],"names":["normalizeDate"],"mappings":";;AAwBO,SAAS,WACd,MACA,SAAqB,cACb;AACR,QAAM,IAAIA,gBAAc,IAAI;AAE5B,MAAI,MAAM,EAAE,QAAA,CAAS,GAAG;AACtB,YAAQ,KAAK,mCAAmC,IAAI;AACpD,WAAO;AAAA,EACT;AAEA,QAAM,SAA0C;AAAA,IAC9C,MAAM,EAAE,YAAA;AAAA,IACR,IAAI,QAAQ,EAAE,SAAA,IAAa,CAAC;AAAA,IAC5B,IAAI,QAAQ,EAAE,SAAS;AAAA,IACvB,IAAI,QAAQ,EAAE,UAAU;AAAA,IACxB,IAAI,QAAQ,EAAE,YAAY;AAAA,IAC1B,IAAI,QAAQ,EAAE,WAAA,CAAY;AAAA,EAAA;AAG5B,SAAO,OAAO,QAAQ,wBAAwB,CAAA,UAAS,OAAO,OAAO,KAAK,C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AAO,UAAU,MAAM,MAAM;AAAA,IAC/B,KAAK;AAAA,IACL,KAAK;AACH,aAAO,SAAS,MAAM,MAAM;AAAA,IAC9B;AACE,YAAM,IAAI,MAAM,sCAAsC,IAAI,EAAE;AAAA,EAAA;AAElE;AAQO,SAAS,SACd,OACA,OACQ;AACR,QAAM,KAAK,cAAc,KAAK;AAC9B,QAAM,KAAK,cAAc,KAAK;AAG9B,QAAM,OAAO,KAAK,IAAI,GAAG,eAAe,GAAG,SAAA,GAAY,GAAG,QAAA,CAAS;AACnE,QAAM,OAAO,KAAK,IAAI,GAAG,eAAe,GAAG,SAAA,GAAY,GAAG,QAAA,CAAS;AAEnE,QAAM,WAAW,MAAO,KAAK,KAAK;AAClC,SAAO,KAAK,OAAO,OAAO,QAAQ,QAAQ;AAC5C;AAQO,SAAS,UACd,OACA,OACS;AACT,SAAO,SAAS,OAAO,KAAK,MAAM;AACpC;AAOO,SAAS,QAAQ,MAAuC;AAC7D,SAAO,UAAU,MAAM,oBAAI,MAAM;AACnC;AAKA,SAAS,cAAc,MAAoC;AACzD,MAAI,gBAAgB,MAAM;AACxB,WAAO,IAAI,KAAK,IAAI;AAAA,EACtB;AACA,MAAI,OAAO,SAAS,UAAU;AAC5B,WAAO,IAAI,KAAK,IAAI;AAAA,EACtB;AACA,SAAO,IAAI,KAAK,IAAI;AACtB;AChIO,MAAM,UAAU;;;;;;;;;;;;"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * 通用添加时间方法
3
+ * @param date - 基准日期
4
+ * @param amount - 数量
5
+ * @param unit - 时间单位
6
+ * @returns 新的 Date 对象
7
+ * @example
8
+ * ```ts
9
+ * add(new Date(), 1, 'week') // 一周后
10
+ * add(new Date(), -2, 'month') // 两个月前
11
+ * ```
12
+ */
13
+ export declare function add(date: Date | number | string, amount: number, unit: TimeUnit): Date;
14
+
15
+ /**
16
+ * 在指定日期上添加天数
17
+ * @param date - 基准日期
18
+ * @param days - 要添加的天数(可为负数)
19
+ * @returns 新的 Date 对象
20
+ * @example
21
+ * ```ts
22
+ * addDays(new Date('2024-01-15'), 5) // Date('2024-01-20')
23
+ * addDays(new Date('2024-01-15'), -3) // Date('2024-01-12')
24
+ * ```
25
+ */
26
+ export declare function addDays(date: Date | number | string, days: number): Date;
27
+
28
+ /**
29
+ * 在指定日期上添加月份
30
+ * @param date - 基准日期
31
+ * @param months - 要添加的月数(可为负数)
32
+ * @returns 新的 Date 对象
33
+ */
34
+ export declare function addMonths(date: Date | number | string, months: number): Date;
35
+
36
+ /**
37
+ * 在指定日期上添加年份
38
+ * @param date - 基准日期
39
+ * @param years - 要添加的年数(可为负数)
40
+ * @returns 新的 Date 对象
41
+ */
42
+ export declare function addYears(date: Date | number | string, years: number): Date;
43
+
44
+ /**
45
+ * 日期格式化模块
46
+ */
47
+ export declare type DateFormat = 'YYYY-MM-DD' | 'YYYY/MM/DD' | 'YYYY-MM-DD HH:mm:ss' | 'YYYY/MM/DD HH:mm:ss' | 'HH:mm:ss' | 'HH:mm' | string;
48
+
49
+ /**
50
+ * 计算两个日期之间的天数差
51
+ * @param date1 - 第一个日期
52
+ * @param date2 - 第二个日期
53
+ * @returns 天数差(date2 - date1)
54
+ */
55
+ export declare function diffDays(date1: Date | number | string, date2: Date | number | string): number;
56
+
57
+ /**
58
+ * 将日期格式化为指定格式的字符串
59
+ * @param date - 要格式化的日期(Date 对象、时间戳或日期字符串)
60
+ * @param format - 格式化模板,默认为 'YYYY-MM-DD'
61
+ * @returns 格式化后的日期字符串
62
+ * @example
63
+ * ```ts
64
+ * formatDate(new Date(), 'YYYY-MM-DD') // '2024-01-15'
65
+ * formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss') // '2024-01-15 14:30:00'
66
+ * ```
67
+ */
68
+ export declare function formatDate(date: Date | number | string, format?: DateFormat): string;
69
+
70
+ /**
71
+ * 获取当前日期的格式化字符串
72
+ * @param format - 格式化模板
73
+ * @returns 格式化后的当前日期字符串
74
+ */
75
+ export declare function getNow(format?: DateFormat): string;
76
+
77
+ /**
78
+ * 获取今天的日期字符串
79
+ * @param format - 格式化模板,默认为 'YYYY-MM-DD'
80
+ * @returns 今天的日期字符串
81
+ */
82
+ export declare function getToday(format?: DateFormat): string;
83
+
84
+ /**
85
+ * 判断是否为同一天
86
+ * @param date1 - 第一个日期
87
+ * @param date2 - 第二个日期
88
+ * @returns 是否为同一天
89
+ */
90
+ export declare function isSameDay(date1: Date | number | string, date2: Date | number | string): boolean;
91
+
92
+ /**
93
+ * 判断日期是否为今天
94
+ * @param date - 要判断的日期
95
+ * @returns 是否为今天
96
+ */
97
+ export declare function isToday(date: Date | number | string): boolean;
98
+
99
+ /**
100
+ * 日期计算模块
101
+ */
102
+ export declare type TimeUnit = 'day' | 'days' | 'week' | 'weeks' | 'month' | 'months' | 'year' | 'years';
103
+
104
+ export declare const version = "__VERSION__";
105
+
106
+ export { }
package/dist/index.mjs ADDED
@@ -0,0 +1,110 @@
1
+ function formatDate(date, format = "YYYY-MM-DD") {
2
+ const d = normalizeDate$1(date);
3
+ if (isNaN(d.getTime())) {
4
+ console.warn("[DateUtils] Invalid date input:", date);
5
+ return "";
6
+ }
7
+ const tokens = {
8
+ YYYY: d.getFullYear(),
9
+ MM: padZero(d.getMonth() + 1),
10
+ DD: padZero(d.getDate()),
11
+ HH: padZero(d.getHours()),
12
+ mm: padZero(d.getMinutes()),
13
+ ss: padZero(d.getSeconds())
14
+ };
15
+ return format.replace(/YYYY|MM|DD|HH|mm|ss/g, (match) => String(tokens[match]));
16
+ }
17
+ function normalizeDate$1(date) {
18
+ if (date instanceof Date) {
19
+ return date;
20
+ }
21
+ if (typeof date === "number") {
22
+ return new Date(date);
23
+ }
24
+ const parsed = new Date(date);
25
+ return parsed;
26
+ }
27
+ function padZero(num) {
28
+ return num < 10 ? `0${num}` : String(num);
29
+ }
30
+ function getNow(format = "YYYY-MM-DD HH:mm:ss") {
31
+ return formatDate(/* @__PURE__ */ new Date(), format);
32
+ }
33
+ function getToday(format = "YYYY-MM-DD") {
34
+ return formatDate(/* @__PURE__ */ new Date(), format);
35
+ }
36
+ function addDays(date, days) {
37
+ const d = normalizeDate(date);
38
+ const result = new Date(d);
39
+ result.setDate(result.getDate() + days);
40
+ return result;
41
+ }
42
+ function addMonths(date, months) {
43
+ const d = normalizeDate(date);
44
+ const result = new Date(d);
45
+ result.setMonth(result.getMonth() + months);
46
+ return result;
47
+ }
48
+ function addYears(date, years) {
49
+ const d = normalizeDate(date);
50
+ const result = new Date(d);
51
+ result.setFullYear(result.getFullYear() + years);
52
+ return result;
53
+ }
54
+ function add(date, amount, unit) {
55
+ const normalizedUnit = unit.toLowerCase();
56
+ switch (normalizedUnit) {
57
+ case "day":
58
+ case "days":
59
+ return addDays(date, amount);
60
+ case "week":
61
+ case "weeks":
62
+ return addDays(date, amount * 7);
63
+ case "month":
64
+ case "months":
65
+ return addMonths(date, amount);
66
+ case "year":
67
+ case "years":
68
+ return addYears(date, amount);
69
+ default:
70
+ throw new Error(`[DateUtils] Unsupported time unit: ${unit}`);
71
+ }
72
+ }
73
+ function diffDays(date1, date2) {
74
+ const d1 = normalizeDate(date1);
75
+ const d2 = normalizeDate(date2);
76
+ const utc1 = Date.UTC(d1.getFullYear(), d1.getMonth(), d1.getDate());
77
+ const utc2 = Date.UTC(d2.getFullYear(), d2.getMonth(), d2.getDate());
78
+ const msPerDay = 1e3 * 60 * 60 * 24;
79
+ return Math.floor((utc2 - utc1) / msPerDay);
80
+ }
81
+ function isSameDay(date1, date2) {
82
+ return diffDays(date1, date2) === 0;
83
+ }
84
+ function isToday(date) {
85
+ return isSameDay(date, /* @__PURE__ */ new Date());
86
+ }
87
+ function normalizeDate(date) {
88
+ if (date instanceof Date) {
89
+ return new Date(date);
90
+ }
91
+ if (typeof date === "number") {
92
+ return new Date(date);
93
+ }
94
+ return new Date(date);
95
+ }
96
+ const version = "__VERSION__";
97
+ export {
98
+ add,
99
+ addDays,
100
+ addMonths,
101
+ addYears,
102
+ diffDays,
103
+ formatDate,
104
+ getNow,
105
+ getToday,
106
+ isSameDay,
107
+ isToday,
108
+ version
109
+ };
110
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/format.ts","../src/calculate.ts","../src/index.ts"],"sourcesContent":["/**\n * 日期格式化模块\n */\n\nexport type DateFormat =\n | 'YYYY-MM-DD'\n | 'YYYY/MM/DD'\n | 'YYYY-MM-DD HH:mm:ss'\n | 'YYYY/MM/DD HH:mm:ss'\n | 'HH:mm:ss'\n | 'HH:mm'\n | string;\n\n/**\n * 将日期格式化为指定格式的字符串\n * @param date - 要格式化的日期(Date 对象、时间戳或日期字符串)\n * @param format - 格式化模板,默认为 'YYYY-MM-DD'\n * @returns 格式化后的日期字符串\n * @example\n * ```ts\n * formatDate(new Date(), 'YYYY-MM-DD') // '2024-01-15'\n * formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss') // '2024-01-15 14:30:00'\n * ```\n */\nexport function formatDate(\n date: Date | number | string,\n format: DateFormat = 'YYYY-MM-DD'\n): string {\n const d = normalizeDate(date);\n\n if (isNaN(d.getTime())) {\n console.warn('[DateUtils] Invalid date input:', date);\n return '';\n }\n\n const tokens: Record<string, string | number> = {\n YYYY: d.getFullYear(),\n MM: padZero(d.getMonth() + 1),\n DD: padZero(d.getDate()),\n HH: padZero(d.getHours()),\n mm: padZero(d.getMinutes()),\n ss: padZero(d.getSeconds()),\n };\n\n return format.replace(/YYYY|MM|DD|HH|mm|ss/g, match => String(tokens[match]));\n}\n\n/**\n * 标准化日期输入为 Date 对象\n */\nfunction normalizeDate(date: Date | number | string): Date {\n if (date instanceof Date) {\n return date;\n }\n if (typeof date === 'number') {\n return new Date(date);\n }\n // 尝试解析字符串\n const parsed = new Date(date);\n return parsed;\n}\n\n/**\n * 数字补零\n */\nfunction padZero(num: number): string {\n return num < 10 ? `0${num}` : String(num);\n}\n\n/**\n * 获取当前日期的格式化字符串\n * @param format - 格式化模板\n * @returns 格式化后的当前日期字符串\n */\nexport function getNow(format: DateFormat = 'YYYY-MM-DD HH:mm:ss'): string {\n return formatDate(new Date(), format);\n}\n\n/**\n * 获取今天的日期字符串\n * @param format - 格式化模板,默认为 'YYYY-MM-DD'\n * @returns 今天的日期字符串\n */\nexport function getToday(format: DateFormat = 'YYYY-MM-DD'): string {\n return formatDate(new Date(), format);\n}\n","/**\n * 日期计算模块\n */\n\nexport type TimeUnit = 'day' | 'days' | 'week' | 'weeks' | 'month' | 'months' | 'year' | 'years';\n\n/**\n * 在指定日期上添加天数\n * @param date - 基准日期\n * @param days - 要添加的天数(可为负数)\n * @returns 新的 Date 对象\n * @example\n * ```ts\n * addDays(new Date('2024-01-15'), 5) // Date('2024-01-20')\n * addDays(new Date('2024-01-15'), -3) // Date('2024-01-12')\n * ```\n */\nexport function addDays(date: Date | number | string, days: number): Date {\n const d = normalizeDate(date);\n const result = new Date(d);\n result.setDate(result.getDate() + days);\n return result;\n}\n\n/**\n * 在指定日期上添加月份\n * @param date - 基准日期\n * @param months - 要添加的月数(可为负数)\n * @returns 新的 Date 对象\n */\nexport function addMonths(date: Date | number | string, months: number): Date {\n const d = normalizeDate(date);\n const result = new Date(d);\n result.setMonth(result.getMonth() + months);\n return result;\n}\n\n/**\n * 在指定日期上添加年份\n * @param date - 基准日期\n * @param years - 要添加的年数(可为负数)\n * @returns 新的 Date 对象\n */\nexport function addYears(date: Date | number | string, years: number): Date {\n const d = normalizeDate(date);\n const result = new Date(d);\n result.setFullYear(result.getFullYear() + years);\n return result;\n}\n\n/**\n * 通用添加时间方法\n * @param date - 基准日期\n * @param amount - 数量\n * @param unit - 时间单位\n * @returns 新的 Date 对象\n * @example\n * ```ts\n * add(new Date(), 1, 'week') // 一周后\n * add(new Date(), -2, 'month') // 两个月前\n * ```\n */\nexport function add(date: Date | number | string, amount: number, unit: TimeUnit): Date {\n const normalizedUnit = unit.toLowerCase() as TimeUnit;\n\n switch (normalizedUnit) {\n case 'day':\n case 'days':\n return addDays(date, amount);\n case 'week':\n case 'weeks':\n return addDays(date, amount * 7);\n case 'month':\n case 'months':\n return addMonths(date, amount);\n case 'year':\n case 'years':\n return addYears(date, amount);\n default:\n throw new Error(`[DateUtils] Unsupported time unit: ${unit}`);\n }\n}\n\n/**\n * 计算两个日期之间的天数差\n * @param date1 - 第一个日期\n * @param date2 - 第二个日期\n * @returns 天数差(date2 - date1)\n */\nexport function diffDays(\n date1: Date | number | string,\n date2: Date | number | string\n): number {\n const d1 = normalizeDate(date1);\n const d2 = normalizeDate(date2);\n\n // 重置时间为当天零点,避免时区问题\n const utc1 = Date.UTC(d1.getFullYear(), d1.getMonth(), d1.getDate());\n const utc2 = Date.UTC(d2.getFullYear(), d2.getMonth(), d2.getDate());\n\n const msPerDay = 1000 * 60 * 60 * 24;\n return Math.floor((utc2 - utc1) / msPerDay);\n}\n\n/**\n * 判断是否为同一天\n * @param date1 - 第一个日期\n * @param date2 - 第二个日期\n * @returns 是否为同一天\n */\nexport function isSameDay(\n date1: Date | number | string,\n date2: Date | number | string\n): boolean {\n return diffDays(date1, date2) === 0;\n}\n\n/**\n * 判断日期是否为今天\n * @param date - 要判断的日期\n * @returns 是否为今天\n */\nexport function isToday(date: Date | number | string): boolean {\n return isSameDay(date, new Date());\n}\n\n/**\n * 标准化日期输入为 Date 对象\n */\nfunction normalizeDate(date: Date | number | string): Date {\n if (date instanceof Date) {\n return new Date(date);\n }\n if (typeof date === 'number') {\n return new Date(date);\n }\n return new Date(date);\n}\n","/**\n * @boo-dreamer/date-utils\n * 日期工具函数库\n */\n\nexport * from './format';\nexport * from './calculate';\n\n// 版本信息\nexport const version = '__VERSION__';\n"],"names":["normalizeDate"],"mappings":"AAwBO,SAAS,WACd,MACA,SAAqB,cACb;AACR,QAAM,IAAIA,gBAAc,IAAI;AAE5B,MAAI,MAAM,EAAE,QAAA,CAAS,GAAG;AACtB,YAAQ,KAAK,mCAAmC,IAAI;AACpD,WAAO;AAAA,EACT;AAEA,QAAM,SAA0C;AAAA,IAC9C,MAAM,EA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2",
4
+ "description": "日期工具函数库",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "sideEffects": false,
20
+ "keywords": [
21
+ "date",
22
+ "utils",
23
+ "typescript",
24
+ "vue3"
25
+ ],
26
+ "author": "",
27
+ "license": "MIT",
28
+ "engines": {
29
+ "node": ">=18.0.0"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "build": "vite build",
36
+ "dev": "vite build --watch",
37
+ "typecheck": "tsc --noEmit",
38
+ "clean": "rm -rf dist"
39
+ }
40
+ }