fix(common): date pipe gives wrong week number (#37632)

Date pipe is giving wrong week number when used with the date format 'w'. If first week(according to Iso)  has some days in previous year

Fixes #33961

PR Close #37632
This commit is contained in:
Ajit Singh 2020-06-18 20:15:18 +05:30 committed by Andrew Kushnir
parent e36d5b201a
commit ef1fb6dee4
2 changed files with 17 additions and 1 deletions

View File

@ -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
}

View File

@ -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');
});
});
});
}