scoreToBeat,
i,
- currentScore;
+ currentScore,
+ validFormatFound,
+ bestFormatIsValid = false;
if (config._f.length === 0) {
getParsingFlags(config).invalidFormat = true;
for (i = 0; i < config._f.length; i++) {
currentScore = 0;
+ validFormatFound = false;
tempConfig = copyConfig({}, config);
if (config._useUTC != null) {
tempConfig._useUTC = config._useUTC;
tempConfig._f = config._f[i];
configFromStringAndFormat(tempConfig);
- if (!isValid(tempConfig)) {
- continue;
+ if (isValid(tempConfig)) {
+ validFormatFound = true;
}
// if there is any input that was not parsed add a penalty for that format
getParsingFlags(tempConfig).score = currentScore;
- if (scoreToBeat == null || currentScore < scoreToBeat) {
- scoreToBeat = currentScore;
- bestMoment = tempConfig;
+ if (!bestFormatIsValid) {
+ if (scoreToBeat == null || currentScore < scoreToBeat || validFormatFound) {
+ scoreToBeat = currentScore;
+ bestMoment = tempConfig;
+ if (validFormatFound) {
+ bestFormatIsValid = true;
+ }
+ }
+ } else {
+ if (currentScore < scoreToBeat) {
+ scoreToBeat = currentScore;
+ bestMoment = tempConfig;
+ }
}
}
assert.equal(moment('2014-05-05', ['YYYY-MM-DD', moment.ISO_8601]).parsingFlags().iso, false, 'iso: edge case array precedence not iso');
});
+test('strict parsing invalid date against array of formats', function (assert) {
+ var b = moment('2/30/2019 7:00pm', ['M/DD/YYYY h:mma", "MM/DD/YYYY h:mma", "M-D-YYYY h:mma", "MM-D-YYYY h:mma'], true);
+ assert.deepEqual(b.parsingFlags().parsedDateParts, [2019,1,30,7,0], 'strict parsing multiple formats should still select the best format even if the date is invalid');
+});
+
test('string with format - years', function (assert) {
assert.equal(moment('67', 'YY').format('YYYY'), '2067', '67 > 2067');
assert.equal(moment('68', 'YY').format('YYYY'), '2068', '68 > 2068');