@duyameng/npmdemo 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/README.md +32 -0
- package/index.js +32 -0
- package/package.json +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Date Utils
|
|
2
|
+
|
|
3
|
+
一个简单的日期工具库
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @your-username/date-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const { formatDate, getDaysDiff } = require('@your-username/date-utils');
|
|
15
|
+
|
|
16
|
+
// 格式化日期
|
|
17
|
+
const date = new Date();
|
|
18
|
+
console.log(formatDate(date, 'YYYY-MM-DD')); // 2026-03-05
|
|
19
|
+
|
|
20
|
+
// 计算日期差
|
|
21
|
+
const date1 = new Date('2026-01-01');
|
|
22
|
+
const date2 = new Date('2026-03-05');
|
|
23
|
+
console.log(getDaysDiff(date1, date2)); // 63
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## API
|
|
27
|
+
|
|
28
|
+
### formatDate(date, format)
|
|
29
|
+
格式化日期为指定格式
|
|
30
|
+
|
|
31
|
+
### getDaysDiff(date1, date2)
|
|
32
|
+
计算两个日期之间的天数差
|
package/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 格式化日期
|
|
3
|
+
* @param {Date} date - 日期对象
|
|
4
|
+
* @param {string} format - 格式字符串,如 'YYYY-MM-DD'
|
|
5
|
+
* @returns {string} 格式化后的日期字符串
|
|
6
|
+
*/
|
|
7
|
+
function formatDate(date, format = 'YYYY-MM-DD') {
|
|
8
|
+
const year = date.getFullYear();
|
|
9
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
10
|
+
const day = String(date.getDate()).padStart(2, '0');
|
|
11
|
+
|
|
12
|
+
return format
|
|
13
|
+
.replace('YYYY', year)
|
|
14
|
+
.replace('MM', month)
|
|
15
|
+
.replace('DD', day);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 获取两个日期之间的天数差
|
|
20
|
+
* @param {Date} date1 - 开始日期
|
|
21
|
+
* @param {Date} date2 - 结束日期
|
|
22
|
+
* @returns {number} 天数差
|
|
23
|
+
*/
|
|
24
|
+
function getDaysDiff(date1, date2) {
|
|
25
|
+
const diff = Math.abs(date2 - date1);
|
|
26
|
+
return Math.floor(diff / (1000 * 60 * 60 * 24));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = {
|
|
30
|
+
formatDate,
|
|
31
|
+
getDaysDiff
|
|
32
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@duyameng/npmdemo",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "一个简单的日期工具库",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"date",
|
|
11
|
+
"utils",
|
|
12
|
+
"format"
|
|
13
|
+
],
|
|
14
|
+
"author": "your-name",
|
|
15
|
+
"license": "MIT"
|
|
16
|
+
}
|