Reputation: 115
I'm writing an application in Flutter and need to get the date of the most recent Monday.
I've searched all over the web and was unable to find any solution to my problem.
Any help would be greatly appreciated
Upvotes: 11
Views: 16696
Reputation: 1527
Try this one:
var dayOfWeek = 1;
DateTime date = DateTime.now();
var lastMonday = date.subtract(Duration(days: date.weekday - dayOfWeek)).toIso8601String();
With dayOfWeek being 1 for Monday, 2 for Tuesday, and so on.
Upvotes: 11
Reputation: 825
Try this one:
void main()
{
var monday=1;
var now = new DateTime.now();
while(now.weekday!=monday)
{
now=now.subtract(new Duration(days: 1));
}
print('Recent monday $now');
}
Upvotes: 34