MacTim
MacTim

Reputation: 115

How to get date of specific day of the week in Flutter/dart?

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

Answers (2)

James Casia
James Casia

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

muddassir
muddassir

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

Related Questions