Skip to content

Commit 10adb9a

Browse files
authored
Merge pull request #715 from imsyy/dev-fix
🦄 refactor: 重写时间逻辑
2 parents 7ddb5fa + f29b2ab commit 10adb9a

1 file changed

Lines changed: 57 additions & 84 deletions

File tree

src/utils/time.ts

Lines changed: 57 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,129 +7,102 @@ dayjs.extend(duration);
77
dayjs.extend(relativeTime);
88
dayjs.extend(localizedFormat);
99

10-
// 秒转为时间
11-
export const secondsToTime = (seconds: number) => {
12-
if (seconds < 3600) {
13-
return dayjs.duration(seconds, "seconds").format("m:ss");
14-
} else {
15-
return dayjs.duration(seconds, "seconds").format("H:mm:ss");
16-
}
10+
/** 秒转为时间字符串 (m:ss 或 H:mm:ss) */
11+
export const secondsToTime = (seconds: number): string => {
12+
const format = seconds < 3600 ? "m:ss" : "H:mm:ss";
13+
return dayjs.duration(seconds, "seconds").format(format);
1714
};
1815

19-
// 毫秒转为时间
20-
export const msToTime = (milliseconds: number) => {
21-
const dur = dayjs.duration(milliseconds, "milliseconds");
22-
return milliseconds < 3600000 ? dur.format("mm:ss") : dur.format("H:mm:ss");
16+
/** 毫秒转为时间字符串 (mm:ss 或 H:mm:ss) */
17+
export const msToTime = (milliseconds: number): string => {
18+
const format = milliseconds < 3600000 ? "mm:ss" : "H:mm:ss";
19+
return dayjs.duration(milliseconds, "milliseconds").format(format);
2320
};
2421

25-
// 毫秒转为秒
22+
/**
23+
* 将毫秒转换为秒
24+
* @param milliseconds - 毫秒数
25+
* @param decimalPlaces - 保留小数位数,默认为 2
26+
* @returns 转换后的秒数
27+
*/
2628
export const msToS = (milliseconds: number, decimalPlaces: number = 2): number => {
2729
return Number((milliseconds / 1000).toFixed(decimalPlaces));
2830
};
2931

3032
/**
31-
* 格式化时间戳
32-
* @param {number|undefined} timestamp - 要格式化的时间戳(以毫秒为单位)。如果为 `null` 或 `0`,则返回空字符串。
33-
* @param {string} [format="YYYY-MM-DD"] - 可选的时间格式,默认格式为 "YYYY-MM-DD"。可传入任意 dayjs 支持的格式。
34-
* @returns {string} - 根据指定格式返回的日期字符串
33+
* 格式化时间戳,同年省略年份
34+
* @param timestamp 时间戳(毫秒),为空或 0 时返回空字符串
35+
* @param format 时间格式,默认 "YYYY-MM-DD"
3536
*/
3637
export const formatTimestamp = (
3738
timestamp: number | undefined,
3839
format: string = "YYYY-MM-DD",
3940
): string => {
4041
if (!timestamp) return "";
4142
const date = dayjs(timestamp);
42-
const currentYear = dayjs().year();
43-
const year = date.year();
44-
// 如果年份相同
45-
if (year === currentYear) {
46-
return date.format(format.replace("YYYY-", ""));
47-
}
48-
return date.format(format);
43+
const isSameYear = date.year() === dayjs().year();
44+
return date.format(isSameYear ? format.replace("YYYY-", "") : format);
4945
};
5046

51-
// 格式化评论时间戳
47+
/**
48+
* 格式化评论时间戳
49+
* @param timestamp 评论时间戳(毫秒)
50+
* @returns 格式化后的字符串
51+
*/
5252
export const formatCommentTime = (timestamp: number): string => {
53-
const now = dayjs();
54-
const diff = now.diff(dayjs(timestamp), "minute");
55-
if (diff < 1) {
56-
return "刚刚发布";
57-
} else if (diff < 60) {
58-
return `${diff}分钟前`;
59-
} else if (diff < 1440) {
60-
// 1天 = 24小时 * 60分钟
61-
return `${Math.floor(diff / 60)}小时前`;
62-
} else if (diff < 525600) {
63-
// 1年约等于 525600分钟
64-
return dayjs(timestamp).format("MM-DD HH:mm");
65-
} else {
66-
return dayjs(timestamp).format("YYYY-MM-DD HH:mm");
67-
}
53+
const timeNow = dayjs();
54+
const timeComment = dayjs(timestamp);
55+
const diffMinute = timeNow.diff(timeComment, "minute");
56+
57+
if (diffMinute < 1) return "刚刚发布";
58+
if (diffMinute < 60) return `${diffMinute} 分钟前`;
59+
if (diffMinute < 1440) return `${Math.floor(diffMinute / 60)} 小时前`;
60+
61+
// 超过一天:同年只显示日期,跨年显示完整日期
62+
const format = timeComment.year() === timeNow.year() ? "MM-DD HH:mm" : "YYYY-MM-DD HH:mm";
63+
return timeComment.format(format);
6864
};
6965

7066
/**
71-
* 计算进度条移动的距离
72-
* @param {number} currentTime
73-
* @param {number} duration
74-
* @returns {number} 进度条移动的距离,精确到 0.01,最大为 100
67+
* 计算进度条百分比
68+
* @param currentTime 当前时间
69+
* @param duration 总时长
70+
* @returns 进度百分比,精确到 0.01,范围 0-100
7571
*/
7672
export const calculateProgress = (currentTime: number, duration: number): number => {
7773
if (duration === 0) return 0;
7874
const progress = (currentTime / duration) * 100;
7975
return Math.min(Math.round(progress * 100) / 100, 100);
8076
};
8177

82-
/**
83-
* 获取当前时间段的问候语
84-
*/
85-
export const getGreeting = () => {
78+
/** 获取当前时间段的问候语 */
79+
export const getGreeting = (): string => {
8680
const hour = dayjs().hour();
87-
if (hour < 6) {
88-
return "凌晨好";
89-
} else if (hour < 9) {
90-
return "早上好";
91-
} else if (hour < 12) {
92-
return "上午好";
93-
} else if (hour < 14) {
94-
return "中午好";
95-
} else if (hour < 17) {
96-
return "下午好";
97-
} else if (hour < 19) {
98-
return "傍晚好";
99-
} else if (hour < 22) {
100-
return "晚上好";
101-
} else {
102-
return "夜深了";
103-
}
81+
const greetings: [number, string][] = [
82+
[6, "凌晨好"],
83+
[9, "早上好"],
84+
[12, "上午好"],
85+
[14, "中午好"],
86+
[17, "下午好"],
87+
[19, "傍晚好"],
88+
[22, "晚上好"],
89+
[24, "夜深了"],
90+
];
91+
return greetings.find(([limit]) => hour < limit)?.[1] ?? "夜深了";
10492
};
10593

106-
/**
107-
* 是否为当天的6点之前
108-
* @param timestamp 当前时间戳
109-
*/
110-
export const isBeforeSixAM = (timestamp: number) => {
111-
// 当天的早上 6 点
94+
/** 判断时间戳是否在当天6点之前 */
95+
export const isBeforeSixAM = (timestamp: number): boolean => {
11296
const sixAM = dayjs().startOf("day").add(6, "hour");
113-
// 判断输入时间是否在六点之前
114-
const inputTime = dayjs(timestamp);
115-
return inputTime.isBefore(sixAM);
97+
return dayjs(timestamp).isBefore(sixAM);
11698
};
11799

118-
/**
119-
* 将 ISO 8601 格式的时间字符串转换为本地时间
120-
* @param isoString - ISO 8601 格式的时间字符串
121-
* @returns
122-
*/
100+
/** ISO 8601 字符串转本地时间格式 */
123101
export const convertToLocalTime = (isoString: string): string => {
124102
return dayjs(isoString).format("YYYY-MM-DD HH:mm:ss");
125103
};
126104

127-
/**
128-
* 将秒转为 分:秒
129-
* @param seconds 秒数
130-
* @returns 分:秒格式的字符串
131-
*/
105+
/** 秒转为 mm:ss 格式(补零) */
132106
export const convertSecondsToTime = (seconds: number): string => {
133-
// 需要补零
134107
return dayjs.duration(seconds, "seconds").format("mm:ss");
135108
};

0 commit comments

Comments
 (0)