weekOfYear static method

  1. @useResult
int weekOfYear(
  1. int year,
  2. int month,
  3. int day
)

Computes the ordinal week of the year.

A week is between 1 and 53, inclusive.

See weeks per year.

Dates.weekOfYear(2023, 4, 1); // 13

Implementation

@useResult static int weekOfYear(int year, int month, int day) => switch ((dayOfYear(year, month, day) - weekday(year, month, day) + 10) ~/ 7) {
  0 => weekOfYear(year - 1, 12, 28),
  53 when weekday(year, 1, 1) != 4 && weekday(year, 12, 31) != 4 => 1,
  final ordinal => ordinal,
};