From the database I get a time as a string: “13:20:00” and I get a time zone: “America/New_York”
How do convert that time to America/Los_Angeles (would return 10:20:00) or any other time zone?
I am using the moment.js library.
I tried:
const t = "13:20:00"
const tzOut = 'America/New_York'
const tzIn = "America/Los_Angeles"
const m = moment(t,"H:mm:SS").tz(tzIn)
return m.tz(tzOut).format("H:mm:SS");
// Returns "16:20:00"
And this works, but if I change the time zones around:
const t = "13:20:00"
const tzIn = 'America/New_York'
const tzOut = "America/Los_Angeles"
const m = moment(t,"H:mm:SS").tz(tzIn)
return m.tz(tzOut).format("H:mm:SS");
// Returns "13:20:00", should be "10:20:00"
I believe this is because I am in the America/Los_Angeles time zone, moment.js is factoring this in and moment(t,"H:mm:SS").tz(tzIn)
is becoming 16:20:00. I want it to become 13:20:00 but in America/New_York time zone.
So it seems I am on the wrong track – how should I be doing this?