weekOfYear property
int
get
weekOfYear
The ISO 8601 week of year 1..53
.
Algorithm from https://en.wikipedia.org/wiki/ISO_week_date#Algorithms
Implementation
int get weekOfYear {
// Add 3 to always compare with January 4th, which is always in week 1
// Add 7 to index weeks starting with 1 instead of 0
final woy = ((ordinalDate - weekday + 10) ~/ 7);
// If the week number equals zero, it means that the given date belongs to the preceding (week-based) year.
if (woy == 0) {
// The 28th of December is always in the last week of the year
return DateTime(year - 1, 12, 28).weekOfYear;
}
// If the week number equals 53, one must check that the date is not actually in week 1 of the following year
if (woy == 53 &&
DateTime(year, 1, 1).weekday != DateTime.thursday &&
DateTime(year, 12, 31).weekday != DateTime.thursday) {
return 1;
}
return woy;
}