简体   繁体   中英

hello i need to compare 2 dates like start date and end date and if i select a wednesday i need all wednesdays occuring between these dates

I have a scenario where user selects a start date and end date and user also selects a specific day I need to show that specific day with date that occurs between them.

I tried the Intl package difference method but did not work

You can use this method. Takes the start and end date and also the weekday. Note, can pass in 3 as an int or 'DateTime.wednesday' as the argument.

Note, idea based on mirkancal's answer in this thread


List<DateTime> getAllDatesOfAWeekday(
    {required DateTime startDate,
    required DateTime endDate,
    required int weekday}) {
  List<DateTime> allDates = [];

  for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
    if (startDate.add(Duration(days: i)).weekday == weekday) {
      allDates.add(startDate.add(Duration(days: i)));
    }
  }
  return allDates;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM