Skip to content

Commit

Permalink
front: drop force-UTC hack
Browse files Browse the repository at this point in the history
We were using a hack to interpret a Date object as an UTC time:
we were converting the Date to a string, and then adding a 'Z'
suffix. This doesn't work great in practice unfortunately.
Date.toString() returns a string which looks like this:

    "Mon Jun 24 2024 13:15:39 GMT+0200 (Central European Summer Time)"

When we add the 'Z' prefix, it looks like this:

    "Mon Jun 24 2024 13:15:39 GMT+0200 (Central European Summer Time)Z"

Some browsers are lax and just accept the string and ignore the
final 'Z', so the Date object remains with the CEST timezone.
Some browsers are less lax and reject the string, returning an
invalid Date object.

Instead of appending 'Z', use the dayjs utc(true) method to switch
the timezone to UTC without adjusting the actual time.

Closes: #7836
  • Loading branch information
emersion committed Jul 4, 2024
1 parent e4226ff commit c61b95f
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion front/src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function dateTimeFormatting(date: Date, withoutTime: boolean = false) {
default:
locale = 'en';
}
const dateToUTC = dayjs(`${date}Z`); // The 'Z' is to ensure we have an UTC date
// Force interpreting the date in UTC
const dateToUTC = dayjs(date).utc(true);
const dateFormat = withoutTime ? 'D MMM YYYY' : 'D MMM YYYY HH:mm';
const tz = dayjs.tz.guess();
return dateToUTC.locale(locale).tz(tz).format(dateFormat).replace(/\./gi, '');
Expand Down

0 comments on commit c61b95f

Please sign in to comment.