ludwig-web-toolkit 0.1.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 +56 -0
- package/package.json +26 -0
- package/src/index.js +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# web-toolkit
|
|
2
|
+
|
|
3
|
+
我的个人提效工具包,包含常用组件和工具函数。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install web-toolkit
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
### formatDate - 日期格式化
|
|
14
|
+
|
|
15
|
+
格式化日期时间为指定格式的字符串。
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
const { formatDate } = require('web-toolkit');
|
|
19
|
+
|
|
20
|
+
// 格式化当前时间
|
|
21
|
+
console.log(formatDate(new Date()));
|
|
22
|
+
// => '2026-02-07 10:30:45'
|
|
23
|
+
|
|
24
|
+
// 自定义格式
|
|
25
|
+
console.log(formatDate(new Date(), 'YYYY/MM/DD'));
|
|
26
|
+
// => '2026/02/07'
|
|
27
|
+
|
|
28
|
+
// 格式化时间戳
|
|
29
|
+
console.log(formatDate(1704614400000, 'YYYY-MM-DD'));
|
|
30
|
+
// => '2024-01-07'
|
|
31
|
+
|
|
32
|
+
// 格式化日期字符串
|
|
33
|
+
console.log(formatDate('2024-01-01', 'MM/DD/YYYY HH:mm'));
|
|
34
|
+
// => '01/01/2024 08:00'
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 支持的格式占位符
|
|
38
|
+
|
|
39
|
+
- `YYYY` - 四位年份
|
|
40
|
+
- `MM` - 两位月份(01-12)
|
|
41
|
+
- `DD` - 两位日期(01-31)
|
|
42
|
+
- `HH` - 24小时制小时(00-23)
|
|
43
|
+
- `mm` - 分钟(00-59)
|
|
44
|
+
- `ss` - 秒数(00-59)
|
|
45
|
+
|
|
46
|
+
## API
|
|
47
|
+
|
|
48
|
+
### formatDate(date, format)
|
|
49
|
+
|
|
50
|
+
- **date**: `Date | string | number` - 日期对象、时间戳或日期字符串
|
|
51
|
+
- **format**: `string` - 格式化模板,默认值:`'YYYY-MM-DD HH:mm:ss'`
|
|
52
|
+
- **返回值**: `string` - 格式化后的日期字符串
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
MIT © ludwig-chan
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ludwig-web-toolkit",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "我的个人提效工具包,包含常用组件和工具函数",
|
|
5
|
+
"main": "src/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/ludwig-chan/web-toolkit.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"toolkit",
|
|
15
|
+
"utilities",
|
|
16
|
+
"components",
|
|
17
|
+
"productivity",
|
|
18
|
+
"ludwig"
|
|
19
|
+
],
|
|
20
|
+
"author": "ludwig-chan",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/ludwig-chan/web-toolkit/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/ludwig-chan/web-toolkit#readme"
|
|
26
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 格式化日期时间
|
|
3
|
+
* @param {Date|string|number} date - 日期对象、时间戳或日期字符串
|
|
4
|
+
* @param {string} format - 格式化模板,支持以下占位符:
|
|
5
|
+
* YYYY - 四位年份
|
|
6
|
+
* MM - 两位月份
|
|
7
|
+
* DD - 两位日期
|
|
8
|
+
* HH - 24小时制小时
|
|
9
|
+
* mm - 分钟
|
|
10
|
+
* ss - 秒数
|
|
11
|
+
* @returns {string} 格式化后的日期字符串
|
|
12
|
+
* @example
|
|
13
|
+
* formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss')
|
|
14
|
+
* // => '2026-02-07 10:30:45'
|
|
15
|
+
*/
|
|
16
|
+
function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
|
|
17
|
+
const d = new Date(date);
|
|
18
|
+
|
|
19
|
+
if (isNaN(d.getTime())) {
|
|
20
|
+
throw new Error('Invalid date');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const year = d.getFullYear();
|
|
24
|
+
const month = String(d.getMonth() + 1).padStart(2, '0');
|
|
25
|
+
const day = String(d.getDate()).padStart(2, '0');
|
|
26
|
+
const hours = String(d.getHours()).padStart(2, '0');
|
|
27
|
+
const minutes = String(d.getMinutes()).padStart(2, '0');
|
|
28
|
+
const seconds = String(d.getSeconds()).padStart(2, '0');
|
|
29
|
+
|
|
30
|
+
return format
|
|
31
|
+
.replace('YYYY', year)
|
|
32
|
+
.replace('MM', month)
|
|
33
|
+
.replace('DD', day)
|
|
34
|
+
.replace('HH', hours)
|
|
35
|
+
.replace('mm', minutes)
|
|
36
|
+
.replace('ss', seconds);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = { formatDate };
|