Skip to content

Commit 03e872a

Browse files
Merge pull request #3472 from memobank/fix-strict-parsing
fix(date-utils): use current locale for strict parsing
2 parents 1568135 + 31bf0a1 commit 03e872a

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/date_utils.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ export function parseDate(value, dateFormat, locale, strictParsing, minDate) {
8686
if (strictParsing) {
8787
strictParsingValueMatch =
8888
isValid(tryParseDate, minDate) &&
89-
value ===
90-
format(tryParseDate, df, {
91-
awareOfUnicodeTokens: true,
92-
});
89+
value === formatDate(tryParseDate, df, locale);
9390
}
9491
if (isValid(tryParseDate, minDate) && strictParsingValueMatch) {
9592
parsedDate = tryParseDate;
@@ -103,7 +100,7 @@ export function parseDate(value, dateFormat, locale, strictParsing, minDate) {
103100
if (strictParsing) {
104101
strictParsingValueMatch =
105102
isValid(parsedDate) &&
106-
value === format(parsedDate, dateFormat, { awareOfUnicodeTokens: true });
103+
value === formatDate(parsedDate, dateFormat, locale);
107104
} else if (!isValid(parsedDate)) {
108105
dateFormat = dateFormat
109106
.match(longFormattingTokensRegExp)

test/date_utils_test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,16 @@ describe("date_utils", function () {
818818
expect(parseDate(value, dateFormat, null, true)).to.not.be.null;
819819
});
820820

821+
it("should parse date based on locale", () => {
822+
const value = "26/05/1995";
823+
const dateFormat = "P";
824+
825+
const expected = new Date("05/26/1995");
826+
const actual = parseDate(value, dateFormat, "pt-BR", true);
827+
828+
assert(isEqual(actual, expected));
829+
});
830+
821831
it("should parse date that matches one of the formats", () => {
822832
const value = "01/15/2019";
823833
const dateFormat = ["MM/dd/yyyy", "yyyy-MM-dd"];
@@ -846,7 +856,7 @@ describe("date_utils", function () {
846856
expect(parseDate(value, dateFormat, null, false)).to.not.be.null;
847857
});
848858

849-
it("should parse date based on locale", () => {
859+
it("should parse date based on locale without strict parsing", () => {
850860
const value = "26/05/1995";
851861
const dateFormat = "P";
852862

0 commit comments

Comments
 (0)