From f3337974847a2f036fa999a33328814e7bfc83b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Libw=C2=B7I?= Date: Mon, 16 Sep 2024 16:07:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E5=BB=BA=E7=AB=99?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E7=BB=9F=E8=AE=A1=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Libw·I --- src/utils/getTime.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/utils/getTime.js b/src/utils/getTime.js index cf2d25526..5b9c286ef 100644 --- a/src/utils/getTime.js +++ b/src/utils/getTime.js @@ -124,19 +124,21 @@ export const checkDays = () => { // 建站日期统计 export const siteDateStatistics = (startDate) => { const currentDate = new Date(); - const differenceInTime = currentDate.getTime() - startDate.getTime(); - const differenceInDays = differenceInTime / (1000 * 3600 * 24); - const differenceInMonths = differenceInDays / 30; - const differenceInYears = differenceInMonths / 12; - if (differenceInYears >= 1) { - return `本站已经苟活了 ${Math.floor(differenceInYears)} 年 ${Math.floor( - differenceInMonths % 12, - )} 月 ${Math.round(differenceInDays % 30)} 天`; - } else if (differenceInMonths >= 1) { - return `本站已经苟活了 ${Math.floor(differenceInMonths)} 月 ${Math.round( - differenceInDays % 30, - )} 天`; - } else { - return `本站已经苟活了 ${Math.round(differenceInDays)} 天`; + let years = currentDate.getFullYear() - startDate.getFullYear(); + let months = currentDate.getMonth() - startDate.getMonth(); + let days = currentDate.getDate() - startDate.getDate(); + + // 如果天数或月份为负数,则调整天数和月份 + if (days < 0) { + months--; + const lastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 0); + days += lastMonth.getDate(); } + + if (months < 0) { + years--; + months += 12; + } + + return `本站已经苟活了 ${years} 年 ${months} 月 ${days} 天`; };