Home > Blockchain >  How to get week days during period of time in flutter
How to get week days during period of time in flutter

Time:06-25

I am creating an app that includes subscription. Now if the customer places an order from today's date to some period of time, I have to calculate total amount (which would be different for everyday). To do that I would need the number of weekdays (like Mondays, Tuesdays, etc.) Within a period of time in flutter. Would be so kind of you to answer the query.

CodePudding user response:

More specified answer because I use a loop from the start date to the end date.

Hope this is what you need. You can access workingDays.length to receive a count of working days if you need just the number.

  final workingDays = <DateTime>[];
  final currentDate = DateTime.now();
  final orderDate = currentDate.add(Duration(days: 10));
  
  DateTime indexDate = currentDate;
  while (indexDate.difference(orderDate).inDays != 0) {
    final isWeekendDay = indexDate.weekday == DateTime.saturday || indexDate.weekday == DateTime.sunday;
    if (!isWeekendDay) {
      workingDays.add(indexDate);
    }
    
    indexDate = indexDate.add(Duration(days: 1));
  }

CodePudding user response:

Lets consider the user selected a dateTime

DateTime userPickedDate = DateTime.now();

create a list of DateTime

List<DateTime> workingDaysList = [];

Now if the user selects 10 days from today

for(var i == 0; i < 10; i  )
{
  if(userPickedDate.add(Duration(days:1)).weekday <= 5) //1 for Monday, 2 for Tuesday so checking 5 for Friday
  {
    workingDaysList.add(userPickedDate.add(Duration(days:1)));
  }
}

userPickedDate This list will contain all DateTime of working days

  • Related