From c8d264af88051b89be381a513f7ef2c78b604f80 Mon Sep 17 00:00:00 2001 From: Eddasol Date: Fri, 12 Apr 2024 16:22:45 +0200 Subject: [PATCH] Fix mix between UTC and local time --- .../HistoricMissionCard.tsx | 2 +- .../MissionHeader/MissionHeader.tsx | 20 +++++++++++-------- frontend/src/utils/StringFormatting.tsx | 11 +++++++--- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/frontend/src/components/Pages/MissionHistoryPage/HistoricMissionCard.tsx b/frontend/src/components/Pages/MissionHistoryPage/HistoricMissionCard.tsx index 10860accf..aa175e41f 100644 --- a/frontend/src/components/Pages/MissionHistoryPage/HistoricMissionCard.tsx +++ b/frontend/src/components/Pages/MissionHistoryPage/HistoricMissionCard.tsx @@ -33,7 +33,7 @@ const MissionEndTimeDisplay = ({ mission }: MissionProps) => { return ( <> {mission.endTime ? ( - {formatDateTime(new Date(mission.endTime), 'HH:mm:ss - dd/MM/yy')} + {formatDateTime(mission.endTime, 'HH:mm:ss - dd/MM/yy')} ) : ( - )} diff --git a/frontend/src/components/Pages/MissionPage/MissionHeader/MissionHeader.tsx b/frontend/src/components/Pages/MissionPage/MissionHeader/MissionHeader.tsx index c77038003..8c0194675 100644 --- a/frontend/src/components/Pages/MissionPage/MissionHeader/MissionHeader.tsx +++ b/frontend/src/components/Pages/MissionPage/MissionHeader/MissionHeader.tsx @@ -97,19 +97,23 @@ const getStartUsedAndRemainingTime = ( if (mission.endTime) { startTime = mission.startTime - ? formatDateTime(new Date(mission.startTime), 'HH:mm') - : formatDateTime(new Date(mission.endTime), 'HH:mm') + ? formatDateTime(mission.startTime, 'HH:mm') + : formatDateTime(mission.endTime, 'HH:mm') + startDate = mission.startTime - ? formatDateTime(new Date(mission.startTime), 'dd/MM/yyy') - : formatDateTime(new Date(mission.endTime), 'dd/MM/yyy') + ? formatDateTime(mission.startTime, 'dd/MM/yyy') + : formatDateTime(mission.endTime, 'dd/MM/yyy') usedTimeInMinutes = mission.startTime - ? differenceInMinutes(new Date(mission.endTime), new Date(mission.startTime)) + ? differenceInMinutes( + convertUTCDateToLocalDate(mission.endTime), + convertUTCDateToLocalDate(mission.startTime) + ) : 0 remainingTime = 'N/A' } else if (mission.startTime) { - startTime = formatDateTime(new Date(mission.startTime), 'HH:mm') - startDate = formatDateTime(new Date(mission.startTime), 'dd/MM/yyy') - usedTimeInMinutes = differenceInMinutes(Date.now(), convertUTCDateToLocalDate(new Date(mission.startTime))) + startTime = formatDateTime(mission.startTime, 'HH:mm') + startDate = formatDateTime(mission.startTime, 'dd/MM/yyy') + usedTimeInMinutes = differenceInMinutes(Date.now(), convertUTCDateToLocalDate(mission.startTime)) if (estimatedDurationInMinutes) remainingTime = Math.max(estimatedDurationInMinutes - usedTimeInMinutes, 0) + ' ' + translatedMinutes else remainingTime = 'N/A' diff --git a/frontend/src/utils/StringFormatting.tsx b/frontend/src/utils/StringFormatting.tsx index ef7137f29..6d7fabe8e 100644 --- a/frontend/src/utils/StringFormatting.tsx +++ b/frontend/src/utils/StringFormatting.tsx @@ -2,8 +2,13 @@ import { format } from 'date-fns' const millisecondsInADay = 8.64e7 -export const convertUTCDateToLocalDate = (date: Date): Date => - new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000) +export const convertUTCDateToLocalDate = (date: Date): Date => { + // If lastChar is Z, typescript assumes the date to be UTC + const lastChar = date.toString().slice(-1) + if (lastChar === 'Z') return new Date(date) + // If not, typescript assumes the date to be local time + return new Date(new Date(date).getTime() - new Date(date).getTimezoneOffset() * 60 * 1000) +} const formatBackendDateTimeToDate = (date: Date) => new Date(date.toString()) @@ -30,4 +35,4 @@ export const getDeadlineInDays = (deadlineDate: Date): number => new Date(deadlineDate.getTime() - new Date().getTime()).getTime() / millisecondsInADay export const formatDateTime = (dateTime: Date, dateFormat: string): string => - format(convertUTCDateToLocalDate(new Date(dateTime)), dateFormat) + format(convertUTCDateToLocalDate(dateTime), dateFormat)