Running moment.tz.guess() produces the following error:
Uncaught TypeError: Cannot read property 'join' of null(…)OffsetAt @ global.js:11363
userOffsets @ global.js:11408
rebuildGuess @ global.js:11475
guess @ global.js:11496
(anonymous function) @ VM762:2
InjectedScript._evaluateOn @ VM757:875
InjectedScript._evaluateAndWrap @ VM757:808
InjectedScript.evaluate @ VM757:664
OS: Windows 7, Build 7601
Browser: Chrome 47.0.2526.111 m
Expected timezone: Asia/Tokyo
Opening the tests page on the momentjs website with the same environment produces no errors. However, in my code and as well as in the console in the momentjs site, running moment.tz.guess() results in the same error.
One of my users have the same error. I can't reproduce it currently. Last Chrome 48. Firefox wors fine.
Uncaught TypeError: Cannot read property 'join' of null
moment-timezone.js:209
Vilnius timezone
Same Error.
OS: Windows 7, Build 7601
Browser: Chrome 48.0.2564.97 (64-bit) and Opera 25.0.1614.71
OS Timezone: (UTC +3:00) Kaliningrad
Firefox works fine.
Maybe use try catch in offsetAt function? I suppose that "abbr" variable in function contains invalid date. For example, we can do like that:
function OffsetAt(at) {
var timeString = at.toTimeString();
var abbr = timeString.match(/\(.+\)/);
try {
if (abbr && abbr[0]) {
// 17:56:31 GMT-0600 (CST)
// 17:56:31 GMT-0600 (Central Standard Time)
abbr = abbr[0].match(/[A-Z]/g).join('');
} else {
// 17:56:31 CST
abbr = timeString.match(/[A-Z]{3,5}/g)[0];
}
} catch (e) {
abbr = undefined;
}
if (abbr === 'GMT') {
abbr = undefined;
}
this.at = +at;
this.abbr = abbr;
this.offset = at.getTimezoneOffset();
}
We have over a dozen users reporting this error. Is there a timeline for a fix on this?
ref https://github.com/freecodecamp/freecodecamp/issues/6633
@Pitmov What language/character set is your OS set to? I'm wondering if the timeString or toTimeString is using Cyrillic characters or some other character set that is confusing the regex which is looking for [A-Z].
@SaintPeter Yes, it's only reproduces on OS with non-latin character set. Javascript regex doesn't support non-latin symbols out of the box and of course this string abbr[0].match(/[A-Z]/g) returns nothing. I think you are absolutely right. I am planning to check that tomorrow, but now I'm sure that problem in OS language. Oh, It's terrible fact.
Sorry for the delay. .guess() is a brand new API, and moment-timezone is still on 0.x. Despite it being fairly complete, there are bound to be occasional bugs. I'll look into this closer. Thanks.
+1 I ran into this problem today and had to work around it.
Not sure how everyone here is using the new API, but I've seen a few cases like the following:
var m = moment.tz(moment.tz.guess());
In general, _don't do that_. Instead just do:
var m = moment();
The only legitimate reason I can think of for the first example is if you want to use the time zone abbreviations in moment-timezone.
var a = moment().format("z"); // ""
var b = moment.tz(moment.tz.guess()).format("z"); // "PST"
As long a moment-timezone guesses correctly, the abbreviation will work. However, it might still guess incorrectly - so use with caution.
The _primary_ use case for guessing the time zone is to send back the time zone ID to the server for use in server-side code.
In my case, I was guessing the timezone client side for use in a timezone-aware scheduler.
const DEFAULT_TIMEZONE = moment.tz.guess();
But because of this particular error, I've had to rely on something like this:
const DEFAULT_TIMEZONE = guess();
function guess() {
try {
return moment.tz.guess();
} catch (err) {
return 'UTC'; // could also be something like moment.tz.names()[0];
}
}
Even if the underlying code can't truly guess, it at least should not throw. (returning false or null would be the best behavior)
Agreed, which is why it's marked as bug. We should catch this on our side.
Feel free to send a PR.
In my case, I have been guessing timezone to convert data that server send in specific local timezone
moment.tz(messageJson.message.head.date, 'YYYY-MM-DD HH:mm:ss.SSS', options.serverTimezone).tz(moment.tz.guess()).fromNow()
Maybe it's wrong solution and I need to use it in different way.
PR at #303
@Pitmov, in your example, you can just call fromNow. "Now" is calculated by UTC, and compared against the UTC time of the moment, so the result is the same. You don't need to call .tz(moment.tz.guess()) at all.
If you were going to call .format() instead of .fromNow, then you still wouldn't need .tz(moment.tz.guess()). You would just call .local() to convert to the moment back to local mode.
We'll track this in #303. Thanks!
Why not keep this issue open until that PR is merged? Otherwise people subscribed to this issue won't know when the bug if fixed.
This is released in 0.5.1.