Skip to content

Commit f9cf05d

Browse files
committed
[FIX] formats: bypass date format for invalid dates
Some number values (too big, or too small) cannot be coerced into valid js dates. Due to this, applying a date format becomes impossible since we rely on the js date for it. With this commit, we bypass the date format (and apply the 'Automatic ' format) when the number cannot be adapted as a js date. closes #8294 Task: 6032998 X-original-commit: df1ece4 Signed-off-by: Rémi Rahir (rar) <rar@odoo.com> Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
1 parent 344d430 commit f9cf05d

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

src/helpers/format/format.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ export const isDateTimeFormat = memoize(function isDateTimeFormat(format: Format
519519

520520
function applyDateTimeFormat(value: number, internalFormat: DateInternalFormat): FormattedValue {
521521
const jsDate = numberToJsDate(value);
522+
if (isNaN(jsDate.getTime())) {
523+
return value.toString();
524+
}
522525

523526
const isMeridian = internalFormat.tokens.some(
524527
(token) => token.type === "DATE_PART" && token.value === "a"

tests/formats/format_helpers.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,15 @@ describe("formatValue on date and time", () => {
981981
expect(formatValue(value, { format: "yyyy", locale })).toBe(result);
982982
});
983983

984+
test.each([
985+
[-4e190, "-4e+190"],
986+
[+4e190, "4e+190"],
987+
[1e16, "10000000000000000"],
988+
[-1e8, "-100000000"],
989+
])("values that cannot be coerced as jsdate are formatted as automatic", (value, result) => {
990+
expect(formatValue(value, { format: "mm/dd/yyyy", locale })).toBe(result);
991+
});
992+
984993
test.each([
985994
["d", "1"],
986995
["dd", "01"],

0 commit comments

Comments
 (0)