修复自慰日历不准确的Bug

This commit is contained in:
York 2025-02-20 23:15:52 +08:00
parent 7eddb0b5de
commit 1ba7a596f0

View File

@ -83,8 +83,9 @@ export const StatsChart = () => {
*/ */
const generateContributionData = () => { const generateContributionData = () => {
const now = new Date(); const now = new Date();
now.setHours(0, 0, 0, 0);
const startDate = new Date(now); const startDate = new Date(now);
startDate.setDate(now.getDate() - (DAYS_IN_WEEK * WEEKS_TO_SHOW)); startDate.setDate(now.getDate() - (DAYS_IN_WEEK * WEEKS_TO_SHOW - 1));
// 初始化贡献数据数组 // 初始化贡献数据数组
const contributionData = Array(WEEKS_TO_SHOW).fill(0).map(() => const contributionData = Array(WEEKS_TO_SHOW).fill(0).map(() =>
@ -94,14 +95,19 @@ export const StatsChart = () => {
// 统计每天的记录次数 // 统计每天的记录次数
records.forEach(record => { records.forEach(record => {
const recordDate = new Date(record.startTime); const recordDate = new Date(record.startTime);
if (recordDate >= startDate) { recordDate.setHours(0, 0, 0, 0);
const daysSince = Math.floor((now.getTime() - recordDate.getTime()) / (1000 * 60 * 60 * 24));
const weekIndex = WEEKS_TO_SHOW - 1 - Math.floor(daysSince / 7); // 确保记录日期在显示范围内
let dayIndex = recordDate.getDay(); if (recordDate >= startDate && recordDate <= now) {
if (dayIndex === 0) dayIndex = 6; // 将周日从0改为6 // 计算记录日期距离结束日期(今天)的天数
else dayIndex -= 1; // 其他日期减1使周一为0 const daysDiff = Math.floor((now.getTime() - recordDate.getTime()) / (1000 * 60 * 60 * 24));
if (weekIndex >= 0 && weekIndex < WEEKS_TO_SHOW) {
contributionData[weekIndex][dayIndex]++; // 计算在哪一周和哪一天
const weekIndex = Math.floor(daysDiff / DAYS_IN_WEEK);
const dayIndex = daysDiff % DAYS_IN_WEEK;
if (weekIndex >= 0 && weekIndex < WEEKS_TO_SHOW && dayIndex >= 0 && dayIndex < DAYS_IN_WEEK) {
contributionData[WEEKS_TO_SHOW - 1 - weekIndex][DAYS_IN_WEEK - 1 - dayIndex]++;
} }
} }
}); });
@ -404,7 +410,8 @@ export const StatsChart = () => {
}}> }}>
{week.map((count, dayIndex) => { {week.map((count, dayIndex) => {
const date = new Date(); const date = new Date();
date.setDate(date.getDate() - ((WEEKS_TO_SHOW - weekIndex - 1) * 7 + dayIndex)); const daysToSubtract = (weekIndex * 7) + dayIndex;
date.setDate(date.getDate() - (DAYS_IN_WEEK * WEEKS_TO_SHOW - 1) + daysToSubtract);
return ( return (
<Box <Box
key={`${weekIndex}-${dayIndex}`} key={`${weekIndex}-${dayIndex}`}