Skip to content

Commit 50114dd

Browse files
authored
style(CopySongInfo): 始终显示完整的发布日期(当前年份不省略) (#998)
1 parent 5c64302 commit 50114dd

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

src/components/Modal/CopySongInfo.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ const duration = computed(() => {
197197
});
198198
199199
const publishTime = computed(() => {
200-
return songInfo.value?.createTime ? formatTimestamp(songInfo.value.createTime, "YYYY-MM-DD") : "";
200+
const createTime = songInfo.value?.createTime;
201+
return typeof createTime === "number" ? formatTimestamp(createTime, "YYYY-MM-DD", true) : "";
201202
});
202203
203204
const songLink = computed(() => {

src/utils/time.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ export const msToS = (milliseconds: number, decimalPlaces: number = 2): number =
3333
* 格式化时间戳,同年省略年份
3434
* @param timestamp 时间戳(毫秒),为空或 0 时返回空字符串
3535
* @param format 时间格式,默认 "YYYY-MM-DD"
36+
* @param keepSameYear 是否保留同年年份,默认为 false(即同年省略年份)
3637
*/
3738
export const formatTimestamp = (
3839
timestamp: number | undefined,
3940
format: string = "YYYY-MM-DD",
41+
keepSameYear: boolean = false,
4042
): string => {
4143
if (!timestamp) return "";
4244
const date = dayjs(timestamp);
43-
const isSameYear = date.year() === dayjs().year();
44-
return date.format(isSameYear ? format.replace("YYYY-", "") : format);
45+
const shouldOmitYear = !keepSameYear && date.year() === dayjs().year();
46+
return date.format(shouldOmitYear ? format.replace("YYYY-", "") : format);
4547
};
4648

4749
/**

0 commit comments

Comments
 (0)