smalot / bootstrap-datetimepicker

Both Date and Time picker widget based on twitter bootstrap (supports Bootstrap v2 and v3)
http://www.malot.fr/bootstrap-datetimepicker/
Apache License 2.0
3.49k stars 1.65k forks source link

DPGlobal.parseDate fails for Peruvian culture #488

Open jirihelmich opened 8 years ago

jirihelmich commented 8 years ago

As the Peruvian culture has rather strange designators for Meridian (a. m. and p. m.), the library internals fail to parse manually written datetime:

E.g. 25/01/2016 01:00 P. M. is not parsed as a valid date, as there is no matching part in case of the corresponding pattern d/mm/yyyy H:ii P because the part P standing for meridian designator is not matched with parts of the date as parts are parsed in a way that instead of last part being P. M. it has two parts: P and M.

That is caused by matching the regex nonpunctuation (/[^ -\/:-@\[-{-~\t\n\rTZ]+/g`).

AuspeXeu commented 8 years ago

Would you mind filing a PR for this? :)

jirihelmich commented 8 years ago

I'm currently trying to find a fix...

jirihelmich commented 8 years ago
+            var designatorMatch = dates[language].meridiem[1].match(this.nonpunctuation);
 +            var crazyDesignator = designatorMatch && designatorMatch.length > 1;
 +            var crazyDesignatorFound = null;
 +            if (crazyDesignator) {
 +                _.each(dates[language].meridiem, function(designator, idx) {
 +                    if (date.toLowerCase().indexOf(designator.toLowerCase()) > -1) {
 +                        date = date.replace(designator.toLowerCase(), '');
 +                        date = date.replace(designator.toUpperCase(), '');
 +                        crazyDesignatorFound = idx;
 +                        format.parts = _.without(format.parts, 'P', 'p');
 +                    }
 +                });
 +            }

-                var meridian = Math.max(format.parts.indexOf("P"), format.parts.indexOf("p"))
 -                if (meridian != -1) {
 -                    if (parts[meridian].toLowerCase() == dates[language].meridiem[1].toLowerCase()) {
 +                var meridian = Math.max(format.parts.indexOf("P"), format.parts.indexOf("p"));
 +                if (meridian != -1 || crazyDesignatorFound === 1) {
 +                    if (crazyDesignatorFound === 1 || parts[meridian].toLowerCase() == dates[language].meridiem[1].toLowerCase()) {
                          date.setTime(date.getTime() + (12 * 60 * 60 * 1000));
                      }
                  }

This is my patch so far. If the solution seems OK-ish to you, I can file a PR on the latest version of the lib.