From 3ad6aaf307d34311cbe51124a02f8ebc254e8cdd Mon Sep 17 00:00:00 2001 From: Dave Woodward Date: Mon, 15 Dec 2014 16:48:22 -0500 Subject: [PATCH] Clarifies logic that chooses how to parse dates with moment. --- js/datepicker.js | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/js/datepicker.js b/js/datepicker.js index 6200dae2c..7446e1136 100644 --- a/js/datepicker.js +++ b/js/datepicker.js @@ -349,32 +349,38 @@ //some code ripped from http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok parseDate: function(date) { var self = this; - var dt, isoExp, momentParse, month, parts, use; + var BAD_DATE = new Date(NaN); + var dt, isoExp, momentParse, momentParseWithFormat, tryMomentParseAll, month, parts, use; if(date){ if(this.moment){ //if we have moment, use that to parse the dates - momentParse = function(type, d){ - if(type==='b') { - d = moment(d, self.momentFormat); + momentParseWithFormat = function(d) { + var md = moment(d, self.momentFormat); + return (true === md.isValid()) ? md.toDate() : BAD_DATE; + }; + momentParse = function(d) { + var md = moment( new Date(d) ); + return (true === md.isValid()) ? md.toDate() : BAD_DATE; + }; + + tryMomentParseAll = function(d, parseFunc1, parseFunc2) { + var pd = parseFunc1(d); + if (!self.isInvalidDate(pd)) { + return pd; } - else { - // moment shows deprecated warning if sent poorly formated string - // building via "new Date" first - // still possible unpredictable results if the string is kindof well formated - d = moment( new Date(d) ); + pd = parseFunc2(pd); + if (!self.isInvalidDate(pd)) { + return pd; } - - return (d.isValid()===true) ? d.toDate() : new Date(NaN); + return BAD_DATE; }; - use = (typeof(date)==='string') ? ['b', 'a'] : ['a', 'b']; - dt = momentParse(use[0], date); - if(!this.isInvalidDate(dt)){ - return dt; - }else{ - dt = momentParse(use[1], date); - if(!this.isInvalidDate(dt)){ - return dt; - } + + if ('string' === typeof(date)) { + // Attempts to parse date strings using this.momentFormat, falling back on newing a date + return tryMomentParseAll(date, momentParseWithFormat, momentParse); + } else { + // Attempts to parse date by newing a date object directly, falling back on parsing using this.momentFormat + return tryMomentParseAll(date, momentParse, momentParseWithFormat); } }else{ //if moment isn't present, use previous date parsing strategy if(typeof(date)==='string'){