I'm obtaining data.value
which is a time in the format: hh:mm a
- for example 12:30 am
.
I also know:
- the local timezone of the user (
userTimeZone
) - the timezone of the venue (
venueTimeZone
)
I need to convert the time selected by the user (data.value
) to the correct date in the venueTimeZone
. For example, if the user is in Americas/New York
and they selected 1:30PM on the 20/05/2022, and the venue is in Americas/Los Angeles
- the value I am interested in obtaining is 20/05/2022 10:30AM
.
This is my attempt, however the timezone itself doesn't change - I think this is because when I create the userDateTime
with moment
I don't specify a time offset, but I'm not sure how to obtain the offset from userTimeZone
, whilst accounting for DST.
const userTimeZone = _.get(
Intl.DateTimeFormat().resolvedOptions(),
['timeZone']
);
const venueDateStr = new Date().toLocaleString('en-US', {
timeZone: venueTimeZone,
});
const Date = new Date(restaurantDateStr);
const venueYear = venueDate.getFullYear();
const venueMonth = `0${venueDate.getMonth() + 1}`.slice(-2);
const venueDateOfMonth = `0${venueDate.getDate()}`.slice(-2);
const userDateTime = createDateAsUTC(
moment(
`${venueDateOfMonth}/${venueMonth}/${venueYear} ${data.value}`,
'DD/MM/YYYY hh:mm a'
).toDate()
).toLocaleString('en-US', { timeZone: venueTimeZone });
EDIT - I do not have the city offset, I have the timezone name, therefore I cannot use any suggested answer which relies on city offset.