diff --git a/packages/common/src/i18n/format_date.ts b/packages/common/src/i18n/format_date.ts index acb24f99ca..ebeefaedb5 100644 --- a/packages/common/src/i18n/format_date.ts +++ b/packages/common/src/i18n/format_date.ts @@ -711,13 +711,6 @@ export function toDate(value: string|number|Date): Date { if (typeof value === 'string') { value = value.trim(); - const parsedNb = parseFloat(value); - - // any string that only contains numbers, like "1234" but not like "1234hello" - if (!isNaN(value as any - parsedNb)) { - return new Date(parsedNb); - } - if (/^(\d{4}(-\d{1,2}(-\d{1,2})?)?)$/.test(value)) { /* For ISO Strings without time the day, month and year must be extracted from the ISO String before Date creation to avoid time offset and errors in the new Date. @@ -730,6 +723,13 @@ export function toDate(value: string|number|Date): Date { return new Date(y, m - 1, d); } + const parsedNb = parseFloat(value); + + // any string that only contains numbers, like "1234" but not like "1234hello" + if (!isNaN(value as any - parsedNb)) { + return new Date(parsedNb); + } + let match: RegExpMatchArray|null; if (match = value.match(ISO8601_DATE_REGEX)) { return isoStringToDate(match); diff --git a/packages/common/test/i18n/format_date_spec.ts b/packages/common/test/i18n/format_date_spec.ts index fe4dbb83b6..a08ce87ddc 100644 --- a/packages/common/test/i18n/format_date_spec.ts +++ b/packages/common/test/i18n/format_date_spec.ts @@ -61,7 +61,7 @@ describe('Format date', () => { describe('formatDate', () => { const isoStringWithoutTime = '2015-01-01'; const isoStringWithoutTimeOrDate = '2015-01'; - const isoStringWithoutTimeOrDateOrMonth = '2015-01'; + const isoStringWithoutTimeOrDateOrMonth = '2015'; const defaultFormat = 'mediumDate'; let date: Date;