Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with date in different language using datapicker and moments

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
like image 430
Lorraine Bernard Avatar asked Jun 05 '12 19:06

Lorraine Bernard


People also ask

How do I change the language on Datepicker?

Edit file "bootstrap-datepicker.js" to add your languages: Show activity on this post. Download Language wise all datepicker files from here: [https://github.com/jquery/jquery-ui/tree/master/ui/i18n][1] I hope it will work.


1 Answers

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

like image 106
arose Avatar answered Oct 02 '22 08:10

arose