itcast-relative-time 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.
Files changed (4) hide show
  1. package/README.md +45 -0
  2. package/index.js +6 -0
  3. package/package.json +16 -0
  4. package/test.js +2 -0
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # itcast-relative-time
2
+
3
+ 轻量级相对时间格式化工具,集成 dayjs 日期库,将时间戳/Date 对象转换为“刚刚、X分钟前”等人性化文本,适用于评论、订单、消息等时间展示场景。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install itcast-relative-time
9
+ ```
10
+
11
+ ## 使用
12
+
13
+ ```js
14
+ // ES Module 导入
15
+ import formatRelativeTime from 'yourname-relative-time'
16
+
17
+ // 示例 1:传入 Date 对象
18
+ console.log(formatRelativeTime(new Date()))
19
+ // 输出:刚刚
20
+
21
+ // 示例 2:传入毫秒级时间戳
22
+ console.log(formatRelativeTime(Date.now() - 180 * 1000))
23
+ // 输出:3 分钟前
24
+
25
+ // 示例 3:传入秒级时间戳
26
+ console.log(formatRelativeTime(1735324800))
27
+ // 输出:对应的相对时间
28
+ ```
29
+
30
+ ## 时间粒度规则
31
+
32
+ | 时间范围 | 显示文本 |
33
+ | ----------- | ----- |
34
+ | ≤ 10 秒 | 刚刚 |
35
+ | 10 秒 ~ 1 分钟 | X 秒前 |
36
+ | 1 分钟 ~ 1 小时 | X 分钟前 |
37
+ | 1 小时 ~ 1 天 | X 小时前 |
38
+ | 1 天 ~ 30 天 | X 天前 |
39
+ | 30 天 ~ 1 年 | X 个月前 |
40
+ | > 1 年 | X 年前 |
41
+
42
+ ## 参数说明
43
+
44
+ - time(必填):待格式化时间,支持 Date 对象、毫秒级时间戳、秒级时间戳。
45
+ - 返回值:格式化后的相对时间文本字符串。
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import dayjs from "dayjs";
2
+ export function formatRelativeTime() {
3
+ const now = dayjs();
4
+ console.log(now);
5
+ }
6
+ export default formatRelativeTime;
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "itcast-relative-time",
3
+ "version": "1.0.0",
4
+ "description": "轻量级相对时间格式化工具,将时间戳/Date对象转为人性化文本",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "echo \"测试:node test.js\" && node test.js"
9
+ },
10
+ "keywords": ["relative-time", "time-format", "dayjs", "日期格式化"],
11
+ "author": "itcast_njs",
12
+ "license": "MIT",
13
+ "dependencies": {
14
+ "dayjs": "^1.11.10"
15
+ }
16
+ }
package/test.js ADDED
@@ -0,0 +1,2 @@
1
+ import { formatRelativeTime } from './index.js'
2
+ formatRelativeTime()