Question

I have a web site which, depending on the location, allows to set the date in different language:

Example:

Mercredi, Juin 06, 2012 // french
Wednesday, Jun 06, 2012 // english

Then these dates needs to be saved on the server using momentjs

moment('Tuesday, Jun 05, 2012').format(); // 2012-06-05T00:00:00+02:00
moment('Mercredi, Juin 06, 2012').format(); // NaN-NaN-NaNTNaN:NaN:NaN+00:00

How can I fix this issue when the user is using a different language from the english?

P.S.:
Not sure if it can helps...
with momentsjs is possible to set the lang in this way, but the problem persists:

moment.lang('fr');
moment('Mercredi, Juin 06, 2012').format(); // NaN-NaN-NaNTNaN:NaN:NaN+00:00
Était-ce utile?

La solution

There are two things missing:

  1. load the appropriate language file.

  2. To quote from the docu: "You can create a moment from a string that can be parsed by Date.parse" [moment(String)] or "If you know the format of an input string, you can use that to parse a moment" [moment(String, String)]. So, if it isn't understood by Date.parse you need to give the date format as a second argument.

This should work then:

moment.lang("fr");
moment('Mercredi, Juin 06, 2012', "dddd, MMMM DD, YYYY").format();

also see this jsFiddle

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top