Skip to content

Commit 122213d

Browse files
JeanMechethePunderWoman
authored andcommitted
fix(common): The date pipe should return ISO format for week and week-year as intended in the unit test. (#53879)
ISO 8601 defines * Monday as the first day of the week. * week 01 is the week with the first Thursday Therefore: Sunday Dec 31st 2023 is the last day of the last week of the year : W52 2023. PR Close #53879
1 parent a297012 commit 122213d

File tree

4 files changed

+125
-72
lines changed

4 files changed

+125
-72
lines changed

packages/common/src/i18n/format_date.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,20 @@ function getFirstThursdayOfYear(year: number) {
462462
);
463463
}
464464

465-
function getThursdayThisWeek(datetime: Date) {
465+
/**
466+
* ISO Week starts on day 1 (Monday) and ends with day 0 (Sunday)
467+
*/
468+
export function getThursdayThisIsoWeek(datetime: Date) {
469+
// getDay returns 0-6 range with sunday as 0.
470+
const currentDay = datetime.getDay();
471+
472+
// On a Sunday, read the previous Thursday since ISO weeks start on Monday.
473+
const deltaToThursday = currentDay === 0 ? -3 : THURSDAY - currentDay;
474+
466475
return createDate(
467476
datetime.getFullYear(),
468477
datetime.getMonth(),
469-
datetime.getDate() + (THURSDAY - datetime.getDay()),
478+
datetime.getDate() + deltaToThursday,
470479
);
471480
}
472481

@@ -479,7 +488,7 @@ function weekGetter(size: number, monthBased = false): DateFormatter {
479488
const today = date.getDate();
480489
result = 1 + Math.floor((today + nbDaysBefore1stDayOfMonth) / 7);
481490
} else {
482-
const thisThurs = getThursdayThisWeek(date);
491+
const thisThurs = getThursdayThisIsoWeek(date);
483492
// Some days of a year are part of next year according to ISO 8601.
484493
// Compute the firstThurs from the year of this week's Thursday
485494
const firstThurs = getFirstThursdayOfYear(thisThurs.getFullYear());
@@ -496,7 +505,7 @@ function weekGetter(size: number, monthBased = false): DateFormatter {
496505
*/
497506
function weekNumberingYearGetter(size: number, trim = false): DateFormatter {
498507
return function (date: Date, locale: string) {
499-
const thisThurs = getThursdayThisWeek(date);
508+
const thisThurs = getThursdayThisIsoWeek(date);
500509
const weekNumberingYear = thisThurs.getFullYear();
501510
return padNumber(
502511
weekNumberingYear,

0 commit comments

Comments
 (0)