FIx time zone calcs
This commit is contained in:
@@ -216,14 +216,42 @@ export function ChatRoom({ roomId, selectedUserId }: ChatRoomProps) {
|
||||
};
|
||||
|
||||
const formatTime = (timestamp: string) => {
|
||||
const date = new Date(timestamp);
|
||||
// The stored timestamps are actually in Eastern Time but labeled as UTC
|
||||
// We need to compensate by treating them as UTC and then converting back to Eastern
|
||||
const originalDate = new Date(timestamp);
|
||||
|
||||
// Subtract 4 hours to compensate for the EDT offset (adjust to 5 hours for EST months)
|
||||
// This assumes the original data was incorrectly stored as UTC when it was actually EDT/EST
|
||||
const isDST = (date: Date) => {
|
||||
const jan = new Date(date.getFullYear(), 0, 1);
|
||||
const jul = new Date(date.getFullYear(), 6, 1);
|
||||
return date.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
|
||||
};
|
||||
|
||||
// Determine if the timestamp falls in DST period for Eastern Time
|
||||
const offsetHours = isDST(originalDate) ? 4 : 5; // EDT = UTC-4, EST = UTC-5
|
||||
const correctedDate = new Date(originalDate.getTime() - (offsetHours * 60 * 60 * 1000));
|
||||
|
||||
const now = new Date();
|
||||
const isToday = date.toDateString() === now.toDateString();
|
||||
const timeZone = 'America/New_York';
|
||||
|
||||
// Compare dates in Eastern Time to ensure correct "today" detection
|
||||
const dateInET = correctedDate.toLocaleDateString([], { timeZone });
|
||||
const nowInET = now.toLocaleDateString([], { timeZone });
|
||||
const isToday = dateInET === nowInET;
|
||||
|
||||
if (isToday) {
|
||||
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
return correctedDate.toLocaleTimeString([], {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
timeZone
|
||||
});
|
||||
} else {
|
||||
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
return correctedDate.toLocaleDateString([], { timeZone }) + ' ' + correctedDate.toLocaleTimeString([], {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
timeZone
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user