Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

momentJS default locale is zh-tw and can't seem to overwrite it

I'm pulling the momentJS library from a CDN in my Angular app:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>

The default locale is supposed to be English ('en'). But for some reason, the default locale in my app is 'zh-tw'. I see that this used to be an issue (see here and here), but it was supposedly fixed.

Even if I set the global locale manually, it is being ignored:

In my index.html file:

<script>
    moment.locale('en');
</script>

In my Angular controller:

moment.locale('en');

The only thing that works right now is if I set the locale on each moment instance:

var moment1 = moment(myDate);
moment1.locale('en');
var moment2 = moment(moment1).add(24, 'h');
moment2.locale('en');
like image 778
AlexG Avatar asked May 19 '15 22:05

AlexG


People also ask

Why you should not use Momentjs?

Moment. js is a fantastic time & date library with lots of great features and utilities. However, if you are working on a performance sensitive web application, it might cause a huge performance overhead because of its complex APIs and large bundle size.

How do I change locale globally moments?

You can change moment locale globally like that: import moment from 'moment'; import localization from 'moment/locale/fr'; moment. locale('fr', localization);

How do I change my locale in moments?

updateLocale(localeName, config) to change an existing locale. moment. defineLocale(localeName, config) should only be used for creating a new locale. Save this answer.

What is Momentjs?

Moment. js is a stand-alone open-source JavaScript framework wrapper for date objects that eliminates native JavaScript date objects, which are cumbersome to use. Moment. js makes dates and time easy to display, format, parse, validate, and manipulate using a clean and concise API.


2 Answers

If you are using Angular, usually what works for me is this

.run(["moment", function(moment){
    moment.locale("en");
}]);
like image 147
Eugene J. Lee Avatar answered Oct 21 '22 15:10

Eugene J. Lee


I'm aware this question was posted last year but it's worth noting the following for any users coming across the zh-tw formatting issue.

This problem cropped up for me today whilst working with the format method. Luckily there is a more permanent fix to this problem within the last few days.

The issue you're having was originally resolved back in 2013 (approximately) and a subsequent update was released. For whatever reason the problem came back in an update sometime before/around update 2.10.3 and has been an issue since. However, on 2 January 2016 they released an update which has subsequently resolved the problem people were having. Updating to Moment 2.11.0 will resolve the language defaulting to zh-tw and revert it back to en by default.

Whilst the accepted answer was the fix at the time I recommend you update to the latest version of Moment as from my perspective it was a bit of hack around to resolve your issue, amongst other reasons.

Visit the following link for all the changes in Moment 2.11.0, the issue you'll be interested in is under ID #2735.

Moment 2.11.0 - Changelog

like image 2
Damian Avatar answered Oct 21 '22 16:10

Damian