So basically I have a user enter a date and time for an event start. Later on the user picks a location and I want to display that same time but in a different timezone (For example if the user picks 5PM and picks New York as a location, the UTC date should be 5PM in New York 's timezone).
What I am doing right now is the following:
//utcDate is the date the user picked, I'm just extracting its date and time in this test string, I know this is not how I should parse the string but it is just for test purposes. It returns 2021-29-11 18:00:00.
const zonedDateString = `${utcDate.getFullYear()}-${utcDate.getMonth()}-${utcDate.getDate()} ${utcDate.getHours()}:${utcDate.getMinutes()}0:00`
//newTimezone is America/New_York, zonedTimeToUtc is a function from date-fns-tz
const newDate = zonedTimeToUtc(zonedDateString, newTimezone)
// The result for newDate is 2021-11-29T18:00:00Z
Any idea why this isnt working? It is basically just attaching a Z to the end of the string and not adapting it to the timezone.