getDatesFromWeekNumber function

WeekDates getDatesFromWeekNumber(
  1. int year,
  2. int weekNumber
)

Returns the start and end dates of the specified year and week.

Implementation

WeekDates getDatesFromWeekNumber(int year, int weekNumber) {
  final DateTime firstDayOfYear = DateTime.utc(year, 1, 1);

  final int firstDayOfWeek = firstDayOfYear.weekday;

  final int daysToFirstWeek = (8 - firstDayOfWeek) % 7;

  final DateTime firstDayOfGivenWeek = firstDayOfYear
      .add(Duration(days: daysToFirstWeek + (weekNumber - 1) * 7));

  final DateTime lastDayOfGivenWeek =
      firstDayOfGivenWeek.add(const Duration(days: 6));

  return WeekDates(from: firstDayOfGivenWeek, to: lastDayOfGivenWeek);
}