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
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.
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.
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.
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');
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With