diff --git a/lib/parseformat.js b/lib/parseformat.js index 266a38b..a249ba8 100644 --- a/lib/parseformat.js +++ b/lib/parseformat.js @@ -281,19 +281,24 @@ function parseFormat (dateString, options) { format = format.replace(regexMonth, options.preferLongFormat ? 'MM' : 'M') } + // If still remaining more numbers and year is not present then one number is the year + if (!format.match(formatIncludesYear) && format.match(formatHasTimeAfterRemainingDigits)) { + format = format.replace(regexYearShort, 'YY') + } + // Check for some odd cases, such as MMMM - if (format.match(formatHasDayWithLeadingZeroAfterMonthSegment) && !format.match(formatHasTimeAfterRemainingDigits)) { + if (format.match(formatHasDayWithLeadingZeroAfterMonthSegment)) { format = format.replace(regexDayLeadingZero, 'DD') } - if (format.match(formatHasDayAfterMonthSegment) && !format.match(formatHasTimeAfterRemainingDigits)) { + if (format.match(formatHasDayAfterMonthSegment)) { format = format.replace(regexDay, options.preferLongFormat ? 'DD' : 'D') } // now, the next number, if existing, must be a day IF it is the last set of numbers in the format - if (!format.match(formatIncludesNumericDay) && format.match(formatHasMultipleRemainingSegments) && !format.match(formatHasTimeAfterRemainingDigits)) { + if (!format.match(formatIncludesNumericDay) && format.match(formatHasMultipleRemainingSegments) ) { format = format.replace(regexDayLeadingZero, 'DD') } - if (!format.match(formatIncludesNumericDay) && format.match(formatHasMultipleRemainingSegments) && !format.match(formatHasTimeAfterRemainingDigits)) { + if (!format.match(formatIncludesNumericDay) && format.match(formatHasMultipleRemainingSegments) ) { format = format.replace(regexDay, options.preferLongFormat ? 'DD' : 'D') } diff --git a/test/moment-parseformat-test.js b/test/moment-parseformat-test.js index 25daf65..9e61177 100644 --- a/test/moment-parseformat-test.js +++ b/test/moment-parseformat-test.js @@ -3932,6 +3932,6 @@ test('Tons of varios date time tz formats', function (t) { t.equal(moment.parseFormat('Sunday, March 4, 2012 9:00:00 PM+07:00'), 'dddd, MMMM D, YYYY h:mm:ss AZ', 'Sunday, March 4, 2012 9:00:00 PM+07:00 → dddd, MMMM D, YYYY h:mm:ss AZ') t.equal(moment.parseFormat('Sunday, March 4, 2012 9:00:00 pm+0700'), 'dddd, MMMM D, YYYY h:mm:ss aZZ', 'Sunday, March 4, 2012 9:00:00 pm+0700 → dddd, MMMM D, YYYY h:mm:ss aZZ') t.equal(moment.parseFormat('Sunday, March 4, 2012 9:00:00 pm+07:00'), 'dddd, MMMM D, YYYY h:mm:ss aZ', 'Sunday, March 4, 2012 9:00:00 pm+07:00 → dddd, MMMM D, YYYY h:mm:ss aZ') - + t.equal(moment.parseFormat('Sun Mar 23 10:14:18 CET 2021'),'ddd MMM D H:mm:ss [CET] YYYY', 'Sun Mar 23 10:14:18 CET 2021 → ddd MMM D H:mm:ss [CET] YYYY' ) t.end() })