From 73eab55ccdb5f2780e1416e3ee06b132af335740 Mon Sep 17 00:00:00 2001 From: Koloto Date: Mon, 24 Nov 2014 16:38:53 +0300 Subject: [PATCH 1/2] v4: option extraFormats to improve parsing --- src/js/bootstrap-datetimepicker.js | 72 +++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/src/js/bootstrap-datetimepicker.js b/src/js/bootstrap-datetimepicker.js index b24e42e19..0ac23d71a 100644 --- a/src/js/bootstrap-datetimepicker.js +++ b/src/js/bootstrap-datetimepicker.js @@ -60,6 +60,7 @@ use24hours, minViewModeNumber = 0, actualFormat, + parseFormats, currentViewMode, datePickerModes = [ { @@ -955,7 +956,7 @@ if (moment.isMoment(date) || date instanceof Date) { date = moment(date); } else { - date = moment(date, actualFormat, options.useStrict); + date = moment(date, parseFormats, options.useStrict); } date.locale(options.locale); return date; @@ -1023,23 +1024,18 @@ return (Object.keys(givenDatesIndexed).length) ? givenDatesIndexed : false; }, - format = function (newFormat) { - if (arguments.length === 0) { - return options.format; - } - - if ((typeof newFormat !== 'string') && ((typeof newFormat !== 'boolean') || (newFormat !== false))) { - throw new TypeError('format() expects a sting or boolean:false parameter ' + newFormat); - } - - options.format = newFormat; + initFormatting = function () { + var format = options.format || 'L LT'; - newFormat = newFormat || 'L LT'; - - actualFormat = newFormat.replace(/(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g, function (input) { + actualFormat = format.replace(/(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g, function (input) { return date.localeData().longDateFormat(input) || input; }); + parseFormats = options.extraFormats ? options.extraFormats.slice() : []; + if (parseFormats.indexOf(format) < 0 && parseFormats.indexOf(actualFormat) < 0) { + parseFormats.push(actualFormat); + } + use24hours = (actualFormat.toLowerCase().indexOf('a') < 1 && actualFormat.indexOf('h') < 1); if (isEnabled('y')) { @@ -1053,11 +1049,6 @@ } currentViewMode = Math.max(minViewModeNumber, currentViewMode); - - if (!unset) { - setValue(date); - } - return picker; }; /******************************************************************************** @@ -1135,7 +1126,40 @@ return picker; }; - picker.format = format; + picker.format = function (newFormat) { + if (arguments.length === 0) { + return options.format; + } + + if ((typeof newFormat !== 'string') && ((typeof newFormat !== 'boolean') || (newFormat !== false))) { + throw new TypeError('format() expects a sting or boolean:false parameter ' + newFormat); + } + + options.format = newFormat; + if (actualFormat) { + initFormatting(); // reinit formatting + } + if (!unset) { + setValue(date); + } + return picker; + }; + + picker.extraFormats = function (formats) { + if (arguments.length === 0) { + return options.extraFormats; + } + + if (formats !== false && !(formats instanceof Array)) { + throw new TypeError('extraFormats() expects an array or false parameter'); + } + + options.extraFormats = formats; + if (parseFormats) { + initFormatting(); // reinit formatting + } + return picker; + }; picker.disabledDates = function (dates) { if (arguments.length === 0) { @@ -1287,7 +1311,10 @@ options.locale = locale; date.locale(options.locale); viewDate.locale(options.locale); - format(options.format); // re-evaluate actualFormat variable in case options.format is not set + + if (actualFormat) { + initFormatting(); // reinit formatting + } if (widget) { hide(); show(); @@ -1529,6 +1556,8 @@ picker.options(options); + initFormatting(); + attachDatePickerElementEvents(); if (input.prop('disabled')) { @@ -1563,6 +1592,7 @@ $.fn.datetimepicker.defaults = { format: false, + extraFormats: false, stepping: 1, minDate: false, maxDate: false, From 0d3925269e42493a466242cb4e90a13d88dcf92f Mon Sep 17 00:00:00 2001 From: Koloto Date: Mon, 24 Nov 2014 19:47:11 +0300 Subject: [PATCH 2/2] v4: option extraFormats to improve parsing (small fix) --- src/js/bootstrap-datetimepicker.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/js/bootstrap-datetimepicker.js b/src/js/bootstrap-datetimepicker.js index 0ac23d71a..fdf7fd42c 100644 --- a/src/js/bootstrap-datetimepicker.js +++ b/src/js/bootstrap-datetimepicker.js @@ -1049,6 +1049,10 @@ } currentViewMode = Math.max(minViewModeNumber, currentViewMode); + + if (!unset) { + setValue(date); + } }; /******************************************************************************** @@ -1139,9 +1143,6 @@ if (actualFormat) { initFormatting(); // reinit formatting } - if (!unset) { - setValue(date); - } return picker; };