From ae3c2fd29bab7196701a147722544e7e927939ba Mon Sep 17 00:00:00 2001 From: Ryan Tankersley Date: Thu, 12 May 2016 09:58:30 -0500 Subject: [PATCH] When all parsing fails, uses date parsing. If that fails, gives the current date --- js/bootstrap-datetimepicker.js | 13 +++++++++++++ tests/suites/formats.js | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/js/bootstrap-datetimepicker.js b/js/bootstrap-datetimepicker.js index 2ce8791d..4d2f1f24 100644 --- a/js/bootstrap-datetimepicker.js +++ b/js/bootstrap-datetimepicker.js @@ -1536,6 +1536,8 @@ } return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), 0); } + + var originalDate = date; var parts = date && date.toString().match(this.nonpunctuation) || [], date = new Date(0, 0, 0, 0, 0, 0, 0), parsed = {}, @@ -1631,6 +1633,17 @@ setters_map[s](date, parsed[s]) } } + //No parsing worked, so attempt to just use Date's parsing + else { + var dateInstance = new Date(originalDate); + if (dateInstance.toString() !== 'Invalid Date') { + return this.parseDate(dateInstance); + //Not even date parsing worked. Send back the current date time + } else { + return this.parseDate(new Date()); + } + } + return date; }, formatDate: function (date, format, language, type) { diff --git a/tests/suites/formats.js b/tests/suites/formats.js index f20e45b7..78a027e3 100644 --- a/tests/suites/formats.js +++ b/tests/suites/formats.js @@ -245,3 +245,11 @@ test('Untrimmed datetime value', patch_date(function(Date){ .datetimepicker('setValue'); equal(this.input.val(), '2012-03-05 00:00'); })); + +test('When parsing does not work uses date parsing', function(){ + this.input + .val('8/19/1986') + .datetimepicker({format: 'mm/dd/yy HH:ii P'}) + .datetimepicker('setValue'); + equal(this.input.val(), '08/19/86 12:00 AM'); +});