weekday static method

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

Computes the ordinal day of the week.

A week starts on Monday (1) and ends Sunday (7).

Example

Dates.weekday(1969, 7, 20); // 7 (Sunday)

Implementation details

This function uses a modified version of of Tomohiko Sakamoto's algorithm where Monday = 1 and Sunday = 7.

See Sakamoto's methods.

Implementation

@useResult static int weekday(int year, int month, int day) {
  const table = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
  final adjustedYear = month < 3 ? year - 1 : year;
  final weekday = (adjustedYear + adjustedYear ~/4 - adjustedYear ~/100 + adjustedYear ~/400 + table[month - 1] + day) % 7;
  return weekday == 0 ? 7 : weekday;
}