From 0be4e71328bd1344e107a8261c8ae3719ee03f0f Mon Sep 17 00:00:00 2001 From: Han <1655466387@qq.com> Date: Tue, 18 Feb 2025 14:09:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=B6=E9=97=B4=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scripts/Talking.ts | 2 +- src/utils/index.ts | 35 ++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/scripts/Talking.ts b/src/scripts/Talking.ts index 3c67bb2..3c8e010 100644 --- a/src/scripts/Talking.ts +++ b/src/scripts/Talking.ts @@ -18,7 +18,7 @@ const TalkingInit = async (data: any) => { if (typeof data === 'string') { res = await $GET(data); } - talkingDOM.innerHTML = res.map((i: any) => `

.𝙃𝙖𝙣

${i.content}
`).join(''); + talkingDOM.innerHTML = res.map((i: any) => `

.𝙃𝙖𝙣

${i.content}
`).join(''); // 图片懒加载 vhLzImgInit(); // 灯箱JS初始化====== diff --git a/src/utils/index.ts b/src/utils/index.ts index 0b09cc5..d5caff1 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -5,25 +5,30 @@ const getDescription = (post: any, num: number = 150) => (post.rendered ? post.r const fmtTime = (time: any, fmt: string = 'MMMM D, YYYY') => dayjs(time).format(fmt) // 处理日期 const fmtDate = (time: string | Date) => { - const now = dayjs() - const past = dayjs(time) - // 计算时间差组件 - const years = now.diff(past, 'year') - const months = now.diff(past.add(years, 'year'), 'month') - const days = now.diff(past.add(years, 'year').add(months, 'month'), 'day') - const hours = now.diff(past, 'hour') - const minutes = now.diff(past, 'minute') - const seconds = now.diff(past, 'second') - // 构建时间差描述 + const now = dayjs(); + const past = dayjs(time); + // 计算各时间单位,逐步扣除已计算的部分 + const years = now.diff(past, 'year'); + const adjustedPastYears = past.add(years, 'year'); + const months = now.diff(adjustedPastYears, 'month'); + const adjustedPastMonths = adjustedPastYears.add(months, 'month'); + const days = now.diff(adjustedPastMonths, 'day'); + const adjustedPastDays = adjustedPastMonths.add(days, 'day'); + const hours = now.diff(adjustedPastDays, 'hour'); + const adjustedPastHours = adjustedPastDays.add(hours, 'hour'); + const minutes = now.diff(adjustedPastHours, 'minute'); + const adjustedPastMinutes = adjustedPastHours.add(minutes, 'minute'); + const seconds = now.diff(adjustedPastMinutes, 'second'); + // 构建时间差描述,仅在没有更大单位时显示较小单位 return [ years && `${years}年`, months && `${months}月`, days && `${days}天`, - !years && !months && hours && `${hours}小时`, - !years && !months && !days && minutes && `${minutes}分钟`, - !years && !months && !days && !hours && `${seconds}秒` - ].filter(Boolean).join('') -} + hours && !years && !months && `${hours}小时`, + minutes && !years && !months && !days && `${minutes}分`, + seconds && !years && !months && !days && !hours && `${seconds}秒` + ].filter(Boolean).join(''); +}; // 处理页码展示 const fmtPage = (page: string | undefined) => page ? page.replace(/\//g, '') : null