weekOfYear method

int weekOfYear()

Calculates the week number of the year for the current DateTime instance.

The first week of the year starts from January 1st, and each subsequent week begins on a new Monday.

Example Usage:

final date = DateTime(2025, 2, 5);
print(date.weekOfYear()); // Output: 6 (Feb 5th falls in the 6th week of 2025)

Returns:

  • An integer representing the week number of the year.

Note:

  • This method assumes weeks start from January 1st and does not strictly follow ISO 8601 week numbering (which considers the first Thursday of the year).

Implementation

int weekOfYear() {
  final startOfYear = DateTime(year, 1, 1);
  final daysSinceStart = difference(startOfYear).inDays;
  return (daysSinceStart / 7).ceil();
}