timezonemomentjsdstmoment-timezone

moment().tz() is not giving same .isDST() value as moment.tz()


I am facing an issue related to DaylightSaving where time in America/Los_Angeles timezone is not adjusting to the Daylight savings when I use moment(<DateTime>).tz(America/Los_Angeles).

>> let c = moment('2023-03-12 9:15 AM', 'YYYY-MM-DD HH:mm a').tz('America/Los_Angeles', true);
>> c.isDST()
false

But when I use moment.tz(), I am getting the right value.

>> let c = moment.tz('2023-03-12 9:15 AM', 'YYYY-MM-DD HH:mm a', 'America/Los_Angeles');
>> c.isDST()
true

Why are they not equal? Which is the right way of doing it? Is there any other problem with the first approach that I should keep in mind while using it?


Solution

  • The first approach is creating a moment based on local time of the time zone wherever the code happens to be running. It's then converting that moment to Pacific time. The true option states to keep the local time while adjusting the time zone and offset only.

    The second approach is creating a moment specifically based on Pacific time, without involving the local time zone.

    In theory, either version should work. However, there's a bug in moment-timezone that affects the first approach. I would use the second only.