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

v4: option extraFormats to improve parsing #666

Merged
merged 2 commits into from
Nov 25, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions src/js/bootstrap-datetimepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use24hours,
minViewModeNumber = 0,
actualFormat,
parseFormats,
currentViewMode,
datePickerModes = [
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand All @@ -1057,7 +1053,6 @@
if (!unset) {
setValue(date);
}
return picker;
};

/********************************************************************************
Expand Down Expand Up @@ -1135,7 +1130,37 @@
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
}
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) {
Expand Down Expand Up @@ -1287,7 +1312,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();
Expand Down Expand Up @@ -1529,6 +1557,8 @@

picker.options(options);

initFormatting();

attachDatePickerElementEvents();

if (input.prop('disabled')) {
Expand Down Expand Up @@ -1563,6 +1593,7 @@

$.fn.datetimepicker.defaults = {
format: false,
extraFormats: false,
stepping: 1,
minDate: false,
maxDate: false,
Expand Down