Moment.js Time Zone isn't working
I'm trying to utilize the moment.js library in my script.js file, specifically to determine time zone eligibility. I've loaded the moment.js and moment-timezone-wth-data.js files per the momentjs.com website.
Moment seems to be working fine. If I run the following code, everything seems to be working:
var testnow = moment().format('MMM DD h:mm A');
However, if I attempt to format via a time zone (see below), I receive the following error: moment(...).tz is not a function.
var newchatdate = moment().tz("America/Los_Angeles").format();
Any ideas on why the moment timezone javascript file wouldn't be loading appropriately?
-
Hi David,
In the past, there have been conflicts with including moment.js within Zendesk Guide's implementation. While including moment.js doesn't seem to cause a problem anymore, as you found out, including moment-timezone-with-data.js (or one of the related libraries) does.
I'm trying to find out if there are any workarounds to using this library. In the meantime, it does indeed look like there's an issue with including it. I'll post back here when I find out more.
-
I created a workaround by loading the scripts from a CDN, like so:
// Load moment.js remotely from CDN
$.getScript("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js", function() {
// Load the moment.js time zone data remotely
$.getScript("https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.26/moment-timezone.min.js", function() {
// Add time zone(s)
moment.tz.add('America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0');
// Get current EST time
var now = moment().tz("America/New_York");
// Execute rest of script here...
});
}); -
Great workaround David -- I was able to reproduce the original issue by including the libraries via script tags. Your approach does workaround the issue, however. Thanks for sharing!
-
@...
It seems, the bug has popped up again. The moment.js in themes are failing to work -- probably Zendesk is loading another version? We reported it here: https://support.zendesk.com/hc/en-us/community/posts/360031296373-Do-you-use-moment-js-in-your-Theme-Has-it-been-broken-in-last-couple-of-days-
A lot of people have moment.js (old and newer versions) included in theme, and those are being broken. Kindly check it.
Thank you -
Hello Diziana,
We reached out to our engineering team, and it looks like we will have to take this issue in a ticket to further troubleshoot it for you. I'll go ahead and create one on your behalf so expect to hear from a member of our team shortly.
Best regards. -
Thank you. I realized the change was rolled back but we need to discuss and come to a solution so it doesn't happen again. A lot of help-centers are using moment.js for years, and it will be hard to have all them change their code because Zendesk is using a newer moment.js in their own code which some way overrides customer's version of code.
Thank you
-
Diziana, we began having trouble with moment.js also, so we switched over to the lightweight Dayjs for our time calculations.
Please sign in to leave a comment.
7 Comments