Skip to content

Commit

Permalink
Merge pull request #356 from Libw-I/dev
Browse files Browse the repository at this point in the history
fix: 修正建站日期统计算法
  • Loading branch information
imsyy authored Sep 30, 2024
2 parents 3d46072 + f333797 commit 31bbcc3
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/utils/getTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} 天`;
};

0 comments on commit 31bbcc3

Please sign in to comment.