diff --git a/packages/common/src/i18n/format_date.ts b/packages/common/src/i18n/format_date.ts index 70f9f33a9a..355298a60f 100644 --- a/packages/common/src/i18n/format_date.ts +++ b/packages/common/src/i18n/format_date.ts @@ -382,8 +382,10 @@ function weekGetter(size: number, monthBased = false): DateFormatter { const today = date.getDate(); result = 1 + Math.floor((today + nbDaysBefore1stDayOfMonth) / 7); } else { - const firstThurs = getFirstThursdayOfYear(date.getFullYear()); const thisThurs = getThursdayThisWeek(date); + // Some days of a year are part of next year according to ISO 8601. + // Compute the firstThurs from the year of this week's Thursday + const firstThurs = getFirstThursdayOfYear(thisThurs.getFullYear()); const diff = thisThurs.getTime() - firstThurs.getTime(); result = 1 + Math.round(diff / 6.048e8); // 6.048e8 ms per week } diff --git a/packages/common/test/pipes/date_pipe_spec.ts b/packages/common/test/pipes/date_pipe_spec.ts index 47bfb1b4b1..ba532d5e25 100644 --- a/packages/common/test/pipes/date_pipe_spec.ts +++ b/packages/common/test/pipes/date_pipe_spec.ts @@ -71,6 +71,20 @@ import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_refle describe('transform', () => { it('should use "mediumDate" as the default format', () => expect(pipe.transform('2017-01-11T10:14:39+0000')).toEqual('Jan 11, 2017')); + + it('should return first week if some dates fall in previous year but belong to next year according to ISO 8601 format', + () => { + expect(pipe.transform('2019-12-28T00:00:00', 'w')).toEqual('52'); + expect(pipe.transform('2019-12-29T00:00:00', 'w')).toEqual('1'); + expect(pipe.transform('2019-12-30T00:00:00', 'w')).toEqual('1'); + }); + + it('should return first week if some dates fall in previous leap year but belong to next year according to ISO 8601 format', + () => { + expect(pipe.transform('2012-12-29T00:00:00', 'w')).toEqual('52'); + expect(pipe.transform('2012-12-30T00:00:00', 'w')).toEqual('1'); + expect(pipe.transform('2012-12-31T00:00:00', 'w')).toEqual('1'); + }); }); }); }