React Date-Fns Library Not Handling Time Zones Correctly for Date Formatting
Does anyone know how to I'm having trouble with I'm integrating two systems and I'm using the date-fns library (version 2.28.0) in my React application to format dates, and I'm running into issues with dates that are converting incorrectly across different time zones... I have a datetime string coming from my API in UTC format, and I'm trying to display it in the user's local time zone. Here's the code I'm currently using: ```javascript import { format, parseISO } from 'date-fns'; import { zonedTimeToUtc } from 'date-fns-tz'; const DateDisplay = ({ isoString }) => { const utcDate = parseISO(isoString); const localDate = zonedTimeToUtc(utcDate, 'America/New_York'); // User's time zone return <span>{format(localDate, 'MMMM dd, yyyy HH:mm')}</span>; }; ``` The question is that the date is displayed correctly for some time zones, but for others, it shows the wrong hour. For instance, if I pass an ISO string like `"2023-10-10T15:00:00Z"`, it should render as `"October 10, 2023 11:00"` in EDT, but instead, it displays `"October 10, 2023 15:00"`. I've checked the time zone string, and it looks correct. I also confirmed that the input is being parsed correctly. I tried to use `formatInTimeZone` from `date-fns-tz`, but that didn't work as expected either. Hereβs what I did: ```javascript import { formatInTimeZone } from 'date-fns-tz'; const DateDisplay = ({ isoString }) => { return <span>{formatInTimeZone(isoString, 'America/New_York', 'MMMM dd, yyyy HH:mm')}</span>; }; ``` When using `formatInTimeZone`, it resulted in an behavior stating `Invalid time value`. I also verified that my browser's time zone settings are correct. Is there any additional configuration I need to consider, or is there a better way to handle time zones with date-fns? Any insights would be appreciated! The project is a CLI tool built with Javascript. Could this be a known issue? I'm on macOS using the latest version of Javascript. Thanks for taking the time to read this! Any feedback is welcome!