MCPcopy Create free account
hub / github.com/angular/angular / weekGetter

Function weekGetter

packages/common/src/i18n/format_date.ts:535–554  ·  view source on GitHub ↗
(size: number, monthBased = false)

Source from the content-addressed store, hash-verified

533}
534
535function weekGetter(size: number, monthBased = false): DateFormatter {
536 return function (date: Date, locale: string) {
537 let result;
538 if (monthBased) {
539 const nbDaysBefore1stDayOfMonth =
540 new Date(date.getFullYear(), date.getMonth(), 1).getDay() - 1;
541 const today = date.getDate();
542 result = 1 + Math.floor((today + nbDaysBefore1stDayOfMonth) / 7);
543 } else {
544 const thisThurs = getThursdayThisIsoWeek(date);
545 // Some days of a year are part of next year according to ISO 8601.
546 // Compute the firstThurs from the year of this week's Thursday
547 const firstThurs = getFirstThursdayOfYear(thisThurs.getFullYear());
548 const diff = thisThurs.getTime() - firstThurs.getTime();
549 result = 1 + Math.round(diff / 6.048e8); // 6.048e8 ms per week
550 }
551
552 return padNumber(result, size, getLocaleNumberSymbol(locale, NumberSymbol.MinusSign));
553 };
554}
555
556/**
557 * Returns a date formatter that provides the week-numbering year for the input date.

Callers 1

getDateFormatterFunction · 0.85

Calls 4

getLocaleNumberSymbolFunction · 0.90
getThursdayThisIsoWeekFunction · 0.85
getFirstThursdayOfYearFunction · 0.85
padNumberFunction · 0.85

Tested by

no test coverage detected