fix shift to local timezone

This commit is contained in:
lelo 2025-04-01 21:46:30 +00:00
parent fa8796a413
commit ca07a7b1ba

View File

@ -216,19 +216,17 @@
const timeframe = "{{ timeframe }}"; // e.g., 'last24hours', '7days', '30days', or '365days' const timeframe = "{{ timeframe }}"; // e.g., 'last24hours', '7days', '30days', or '365days'
const shiftedLabels = timeframeData.map(item => { const shiftedLabels = timeframeData.map(item => {
if (timeframe === 'last24hours') { if (timeframe === 'last24hours') {
// For 'last24hours', the bucket is an hour in UTC (e.g., "14") // For 'last24hours', the bucket is an hour in local time (e.g., "14")
const utcHour = parseInt(item.bucket, 10); const localHour = parseInt(item.bucket, 10);
const now = new Date(); const now = new Date();
// Use UTC components to get the correct UTC date // Use local date components since the timestamp is already local
const utcStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), utcHour)); const localStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), localHour);
const utcEnd = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), utcHour + 1)); const localEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), localHour + 1);
// Convert to local time strings return `${localStart.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} - ${localEnd.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}`;
const localStart = utcStart.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const localEnd = utcEnd.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
return `${localStart} - ${localEnd}`;
} else if (timeframe === '7days' || timeframe === '30days') { } else if (timeframe === '7days' || timeframe === '30days') {
const utcDate = new Date(item.bucket + 'T00:00:00Z'); // For these timeframes, assume the bucket is already in local date format
return utcDate.toLocaleDateString(); const localDate = new Date(item.bucket);
return localDate.toLocaleDateString();
} else if (timeframe === '365days') { } else if (timeframe === '365days') {
const [year, month] = item.bucket.split('-'); const [year, month] = item.bucket.split('-');
const dateObj = new Date(year, month - 1, 1); const dateObj = new Date(year, month - 1, 1);