Reputation: 5557
I'm trying to convert a date in GMT to PST. The GMT date is 2022-11-16T00:00:00
, therefore the PST date should be 2022-11-15T16:00:00
.
My code is as follows:
const startDateGMT = moment(startTime).tz(myTimezone, true);
console.log('START DATE GMT IS', startDateGMT, startDateGMT.toISOString());
This outputs 8AM on the 16th, instead of 4PM on the 15th:
START DATE GMT IS Moment<2022-11-16T00:00:00-08:00> 2022-11-16T08:00:00.000Z
What am I doing wrong?
Upvotes: 1
Views: 355
Reputation: 30675
You can use the moment.tz
constructor to parse the input time, using the timezone Etc/GMT
as the timezone.
You can then call .tz()
on the resulting date to convert to Pacific time.
The example below creates the dates and formats them along with the relevant UTC offset.
const gmtTime = '2022-11-16T00:00:00';
const gmtDate = moment.tz(gmtTime, 'Etc/GMT');
const pstDate = moment.tz(gmtTime, 'Etc/GMT').tz('US/Pacific');
console.log('GMT Date:', gmtDate.format('YYYY-MM-DD HH:mm:ssZ'))
console.log('PST Date:', pstDate.format('YYYY-MM-DD HH:mm:ssZ'))
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>
Upvotes: 1