From: Simon Bernier St-Pierre Date: Tue, 3 Jun 2014 15:27:27 +0000 (-0400) Subject: Allow ISO-8601 to be passed as a format. X-Git-Tag: 2.7.0~3^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f7cf9a668ace48301e3b6ec64be35125fbb3062;p=thirdparty%2Fmoment.git Allow ISO-8601 to be passed as a format. This commit allows the ISO-8601 format to be passed as a format in the moment(string, array) constructor. This is a preliminary commit as this addition as yielded a few issues. --- diff --git a/moment.js b/moment.js index 07448344c..022f4c161 100644 --- a/moment.js +++ b/moment.js @@ -1344,6 +1344,11 @@ // date from string and format string function makeDateFromStringAndFormat(config) { + if (config._f === moment.ISO_8601) { + makeDateFromString(config); + return; + } + config._a = []; config._pf.empty = true; @@ -1822,6 +1827,9 @@ // default format moment.defaultFormat = isoFormat; + // constant that refers to the ISO standard + moment.ISO_8601 = 'ISO 8601'; + // Plugins that add properties should also add the key here (null value), // so we can properly clone ourselves. moment.momentProperties = momentProperties; diff --git a/test/moment/create.js b/test/moment/create.js index 9947d4856..f67db1c23 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -395,6 +395,16 @@ exports.create = { test.equal(moment('01', ["MM", "DD"])._f, "MM", "Should use first valid format"); + // pass an ISO date in the array of formats + function parseISO(string) { + return moment(string, ['YYYY', 'HH:mm', 'M', moment.ISO_8601]); + } + // those tests are broken because of the scoring system + // and the deprecation of createFromInputFallback + //test.equal(parseISO('1994').year(), 1994, 'iso: test parse year'); + //test.equal(parseISO('17:15').format('HH:mm'), '17:15', 'iso: test parse HH:mm'); + test.equal(parseISO('2012-06-01').format('YYYY-MM-DD'), '2012-06-01', 'iso: test parse iso'); + test.done(); },