firstDayOfWeek property
DateTime
get
firstDayOfWeek
Returns a DateTime instance representing the first day of the week of this DateTime.
Example:
DateTime currentDate = DateTime(2022, 1, 8);
DateTime firstDayOfWeek = currentDate.firstDayOfWeek;
print(firstDayOfWeek); // Output: 2022-01-03 12:00:00.000 (Assuming Monday is the first day of the week)
The firstDayOfWeek
getter returns a new DateTime instance with the same year, month,
and hour as the original date but adjusted to the first day of the week. Daylight Saving
Time is handled by setting the hour to 12:00 Noon rather than the default of Midnight (00:00:00).
The week in this context is considered to start from Monday.
Implementation
DateTime get firstDayOfWeek {
/// Handle Daylight Savings by setting hour to 12:00 Noon
/// rather than the default of Midnight
final day = DateTime.utc(year, month, this.day, 12);
/// Weekday is on a 1-7 scale Monday - Sunday,
var decreaseNum = (day.weekday - 1) % 7;
return subtract(Duration(days: decreaseNum));
}