I’ve been experimenting with moment, installed via npm and I’m having trouble getting some parts of it to work. And I’m not sure if I understand the concepts correctly.
I’ve used moment successfully with meteor and with the corresponding locale-packages from atmosphere. I’m using german locales in my app and use moment.locale('de')
to have german time formats in german (like ‘28. Dezember’ instead of ‘December, 28th’). This works with the the package rzymek:moment-locale-de
. As far as I can tell it does not much: include the german locale.js from moment, and then set the scope of the moment
variable:
// client.js
global = this;
global.moment = moment;
// server.js
global.moment = moment;
Now, I’m not really sure why, but this takes care that moment.locale('de')
works.
When removing all moment packages that are installed via meteor add
, switching to meteor release 1.3-beta.11
and installing moment via npm (aka omitting the wrapper package for meteor) then `moment.locale(‘de’) does not work anymore.
For example:
import moment from 'moment'
Person = class Person {
/**
* @returns {String}
*/
getBirthDateFormatted () {
moment.locale('de')
return moment(this.get('birthDate')).format('LL')
}
}
This code still returns the birthDate of that Person in us format (September 20, 1981). moment.locale('de')
also always returns en.
I’m sure this is a problem of the scope in which moment tries to set it’s locale-settings, but I’m not sure how to fix it. I tried the solution from the rzymek:moment-locale-de
package, by setting saving moment in the global object, but thats not working.
When connection to the server via meteor shell
it kind of works, but only when using let moment = require('moment')
, like here:
// connected via meteor shell
let moment = require('moment')
moment.locale('de')
// 'de'
let myDate = 'Tue Dec 10 1980 00:00:00 GMT+0100 (CET)'
moment(MyDate).format('LL')
// '10. Dezember 1980'
This only works when using require()
instead of the import xy from 'xy'
method. It’s mysterious to me why.
So the question is: How do I get moment.locale() to work as before switching to npm? I’m not sure that I’m approaching this the right way and would appreciate some hints into the right direction.