Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation incorrect: lang().months returns function rather than months array #682

Closed
JasonCust opened this issue Mar 18, 2013 · 7 comments

Comments

@JasonCust
Copy link

Documentation states:

var fr = moment().lang('fr');
fr.lang().months // ["janvier", "février", "mars", ...]

but on a Node.js REPL:

var fr = moment().lang('fr');
fr.lang().months // [Function]

Side note: executing the function throws an error

fr.lang().months()
TypeError: Cannot call method 'month' of undefined
    at Object.Language.months (./node_modules/moment/moment.js:352:35)
    at repl:1:12
    at REPLServer.self.eval (repl.js:109:21)
    at Interface.<anonymous> (repl.js:248:12)
    at Interface.EventEmitter.emit (events.js:96:17)
    at Interface._onLine (readline.js:200:10)
    at Interface._line (readline.js:518:8)
    at Interface._ttyWrite (readline.js:736:14)
    at ReadStream.onkeypress (readline.js:97:10)
    at ReadStream.EventEmitter.emit (events.js:126:20)
    at emitKey (readline.js:1058:12)
    at ReadStream.onData (readline.js:807:7)
@ichernev
Copy link
Contributor

You're correct, the docs are wrong.
If you want to retrieve months in the latest version of node, please use

moment().lang('fr');  // set language
var fr = moment().lang();  // get language
fr._months; // access months array

You can post issues with the documentation to: https://github.com/timrwood/momentjs.com. https://github.com/timrwood/momentjs.com/issues/25 here's one :)

@terrapass
Copy link
Contributor

@ichernev, while this works for most languages, for some, such as Ukrainian and Russian (maybe a few other languages as well), _months just appears to contain English month names, defined in Language.prototype:

moment().lang('ru');
var ru = moment().lang();
ru._months; //["January", "February", ... ] instead of ["январь", "февраль", ...]

@timrwood
Copy link
Member

The Language#months method should be called like so. The first argument should be a moment, the second arg should be a formatting string.

This was added to allow languages to change between different cases of the month name depending on where in the formatting string the month name fell.

Perhaps that should be outlined in the docs as well.

@terrapass
Copy link
Contributor

If I'm not mistaken, by looking at the code, so far only 3 translations (Russian, Ukrainian and Georgian (ka)) actually override this method. The rest just use nominative case all the time (for now).

Anyway, IMO, using _months should be avoided, as it won't work for languages where months() is overridden.

@timrwood
Copy link
Member

Yeah, its probably best to use something like this instead of directly trying to access the array.

moment.lang().months(moment([0, monthIndex]), "");

@CHenrie
Copy link

CHenrie commented Jun 2, 2014

So how should one get the list of weekdays or months to populate a list for selection?
This was closed with out giving a correct way of using the API. The docs are wrong so what is the solution so I can write a multilingual application with out hacking in ru.js

@mikegleasonjr
Copy link

2 years later and the doc is still wrong...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants