@yoooloo42/bean 1.0.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/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import dateFormat from './src/dateFormat.js';
2
+
3
+ const bean = {
4
+ dateFormat
5
+ }
6
+ export default bean
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@yoooloo42/bean",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/13993193075/bean.git"
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "bugs": {
17
+ "url": "https://github.com/13993193075/bean/issues"
18
+ },
19
+ "homepage": "https://github.com/13993193075/bean#readme"
20
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * 将Date对象格式化为指定的字符串格式
3
+ *
4
+ * @param {Date} date - 要格式化的日期对象
5
+ * @param {string} format - 目标格式字符串,例如:"yyyy年MM月dd日", "yyyy/MM/dd HH:mm:ss", "MM-dd HH:mm"
6
+ * @returns {string} 格式化后的日期字符串
7
+ */
8
+ function dateFormat(date, format) {
9
+ if (!(date instanceof Date) || isNaN(date)) {
10
+ // 如果不是有效的Date对象,返回空字符串或抛出错误
11
+ return '';
12
+ }
13
+
14
+ const o = {
15
+ 'M+': date.getMonth() + 1, // 月份
16
+ 'd+': date.getDate(), // 日
17
+ 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 12小时制
18
+ 'H+': date.getHours(), // 24小时制
19
+ 'm+': date.getMinutes(), // 分
20
+ 's+': date.getSeconds(), // 秒
21
+ 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
22
+ 'S': date.getMilliseconds() // 毫秒
23
+ };
24
+
25
+ // 替换年份 'yyyy'
26
+ if (/(y+)/.test(format)) {
27
+ format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
28
+ }
29
+
30
+ // 替换 'AM/PM'
31
+ if (/(A|a)/.test(format)) {
32
+ const ampm = date.getHours() < 12 ? 'AM' : 'PM';
33
+ format = format.replace(RegExp.$1, RegExp.$1 === 'a' ? ampm.toLowerCase() : ampm);
34
+ }
35
+
36
+ // 替换其他时间单位 'MM', 'dd', 'HH' 等
37
+ for (let k in o) {
38
+ if (new RegExp("(" + k + ")").test(format)) {
39
+ const value = o[k];
40
+ // $1 匹配到的字符串,例如 'MM'
41
+ // 如果是毫秒 'S',则不补零
42
+ format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (value) : (("00" + value).substr(("" + value).length)));
43
+ }
44
+ }
45
+
46
+ return format;
47
+ }
48
+
49
+ /**
50
+ * 计算两个日期之间相差的天数
51
+ *
52
+ * @param {Date} dateFrom - 开始日期 (Date 对象)
53
+ * @param {Date} dateTo - 结束日期 (Date 对象)
54
+ * @returns {number | null} 相差的天数(向下取整)。如果日期无效,则返回 null。
55
+ * 结果为正数表示 dateTo 在 dateFrom 之后;负数表示 dateFrom 在 dateTo 之后。
56
+ */
57
+ function days(dateFrom, dateTo) {
58
+ // 1. 验证输入是否为有效的 Date 对象
59
+ if (!(dateFrom instanceof Date) || isNaN(dateFrom) ||
60
+ !(dateTo instanceof Date) || isNaN(dateTo)) {
61
+ console.error("输入参数必须是有效的 Date 对象。");
62
+ return null;
63
+ }
64
+
65
+ // 2. 计算两个日期的时间戳差值 (毫秒)
66
+ // Date.getTime() 返回从 1970 年 1 月 1 日 00:00:00 UTC 至今的毫秒数
67
+ const timeDifference = dateTo.getTime() - dateFrom.getTime();
68
+
69
+ // 3. 定义一天对应的毫秒数
70
+ const msPerDay = 1000 * 60 * 60 * 24;
71
+
72
+ // 4. 将毫秒差值转换为天数
73
+ // Math.floor() 用于向下取整,确保得到完整的经过天数
74
+ const dayDifference = Math.floor(timeDifference / msPerDay);
75
+
76
+ return dayDifference;
77
+ }
78
+
79
+ const bean = {
80
+ dateFormat,
81
+ days
82
+ }
83
+ export default bean