weekNumber method

int weekNumber()

Returns the ISO 8601 week number, correctly handling year boundaries.

Dates in early January that belong to the last week of the previous year return that year's final week number. Dates in late December that belong to week 1 of the next year return 1.

Prefer this over weekOfYear for any user-facing or standards-compliant week calculations.

Implementation

int weekNumber() {
  if (weekOfYear < 1) return numOfWeeks(year - 1);
  if (weekOfYear > numOfWeeks(year)) return 1;
  return weekOfYear;
}