Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 529
MacTim Avatar asked Mar 15 '19 11:03

MacTim


People also ask

How do you find the day today in Flutter?

We can use the DateTime. now() function to get the current date in Flutter. You do not need to import anything to use the DateTime module. This is an in-built module of Dart and you can use its now() function to get the current date with time.

How do you get a week start date and end in Flutter?

In the Flutter Date Range Picker, you can get the start and end date of the selected range by using the startDate and endDate property of the onSelectionChanged callback args.

How can I get date in dd mm yyyy format in Flutter?

To format DateTime in Flutter using a standard format, you need to use the intl library and then use the named constructors from the DateFormat class. Simply write the named constructor and then call the format() method with providing the DateTime.


2 Answers

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');
}
like image 161
muddassir Avatar answered Oct 19 '22 12:10

muddassir


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.

like image 10
James Casia Avatar answered Oct 19 '22 11:10

James Casia