'Why is isBefore() not working with a different timeszone in dayjs()?

I have an expiry date time (expiryDate which is in mysql date time format) which is always in the Mountain Standard time zone. I need to see if we have passed the expiry date in the browser - which may be in a different time zone.

Here is my code:

let expireTimeMountain = new dayjs(expiryDate, 'YYYY-MM-DD HH:mm:ss');
let currentTimeMountain = new dayjs().tz("America/Edmonton");

let expiredFlag = expireTimeMountain.isBefore(currentTimeMountain);

but this does not work. I think the currentTimeMountain is not being processed in the correct time zone.

Here is my debug code:

console.log(expiredFlag);

console.log(expireTimeMountain.format('ddd, MMM D, YYYY h:mm A'));
console.log(currentTimeMountain.format('ddd, MMM D, YYYY h:mm A'));

and this returns:

true
Fri, Feb 25, 2022 9:05 AM
Fri, Feb 25, 2022 8:48 AM

and so my expiry time is 9:05am and the current time is 8:48am, so expiry flag should be false.

Can anyone see my issue? Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source