I have three separate variables I want to use to construct an ISO 8601 datetime string in UTC (without offsets). The three variables are:
- Date (in
2024-7-31
format) - Time (in
14:00
format) - Timezone (can be one of four values –
America/New_York
,America/Chicago
,America/Denver
,America/Los_Angeles
)
The timezone of the current user (current browser + operating system) should be irrelevant. The user can be in one of the above 4 timezones or any timezone in the world.
I’ve tried using:
import moment from 'moment';
import 'moment-timezone';
const startDate = '2024-7-31';
const startTime = '14:00';
const startTimezone = 'America/Chicago'
console.log(moment.tz(startDate + ' ' + startTime, startTimezone).utc().format());
The above generates 2024-07-31T14:00:00Z
– which equals to 9:00 AM Chicago time on 7-31. The time should be 2:00 PM Chicago time on 7-31. If I change startTimezone
to America/New_York
, I get the same ISO 8601 result (9:00 AM Chicago time).
From the looks of it, it seems like Moment isn’t taking into account the timezone I am feeding it. How do I get Moment to take into account the timezone it is being given?