enter code here
I am trying to make a function that parse event date and present it to use based on his timezone. SO this is the function:
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
dayjs.extend(utc);
dayjs.extend(timezone);
export const parseEventDate = (
timeZone: string,
eventData: {
date: string;
time: string;
timeZone: string;
}
) => {
const eventDateTime = `${eventData.date} ${eventData.time}`;
const gathering = dayjs.tz(eventDateTime, eventData.timeZone);
const eventInLocalTime = gathering.tz(timeZone);
const formattedEventTime = eventInLocalTime.format("YYYY-MM-DD HH:mm");
return formattedEventTime;
};
This is how I test it:
log(
"DATE 1",
parseEventDate("Europe/Belgrade", {
date: "2024-08-21",
time: "16:00",
timeZone: "Europe/Belgrade",
})
);
log(
"DATE 2",
parseEventDate("America/New_York", {
date: "2024-08-21",
time: "16:00",
timeZone: "Europe/Belgrade",
})
);
But instead of date time presented in timezone of the user I get for both: "2024-08-21 14:38"
.
I bang my head whole morning on this.
If it’s important I am using this in react-native and I am getting user timezone from phone but in this case (1st parameter) I hardcoded it to test.
Second parameter is object that contain information about event when and in what timezone it’s saved.
It should be different for New_York.