Home > Enterprise >  Flutter - need dates of all 7 saturdays
Flutter - need dates of all 7 saturdays

Time:09-23

I m trying to get dates of all 7 Saturdays less than today's date.

Means today's date is 22 September so need all 7 Saturdays i.e 18/09/2021,11/09/2021,04/09/2021,28/08/2021,21/08/2021,14/08/2021,07/08/2021.

so this is the code I tried but in this, I am getting a date printed twice.

import 'package:intl/intl.dart';

void main() {
  
  findLastOfPreviousWee2(DateTime dateTime) {
    var dayOfWeek = 6;
    print(dateTime.weekday);
    return dateTime.subtract(Duration(days: dateTime.weekday - dayOfWeek));
  }
  findLastOfPreviousWee1(DateTime dateTime) {
    var sameWeekDayOfLastWeek =
    dateTime.subtract(const Duration(days: 6));
    print(sameWeekDayOfLastWeek);
    return findLastOfPreviousWee2(sameWeekDayOfLastWeek);
  }
  
  findLastOfPreviousWeek(DateTime dateTime) {
    var sameWeekDayOfLastWeek =
    dateTime.subtract(const Duration(days: 6));
    print(sameWeekDayOfLastWeek);
    return findLastOfPreviousWee1(sameWeekDayOfLastWeek);
  }
  
  findLastOfPreviousWeek1(DateTime dateTime) {
    var sameWeekDayOfLastWeek =
    dateTime.subtract(const Duration(days: 6));
    print(sameWeekDayOfLastWeek);
    return findLastOfPreviousWeek(sameWeekDayOfLastWeek);
  }
  
  findLastOfPreviousWeek2(DateTime dateTime) {
    var sameWeekDayOfLastWeek =
    dateTime.subtract(const Duration(days: 6));
    print(sameWeekDayOfLastWeek);
    return findLastOfPreviousWeek1(sameWeekDayOfLastWeek);
  }
  
  findLastOfPreviousWeek3(DateTime dateTime) {
    var sameWeekDayOfLastWeek =
    dateTime.subtract(const Duration(days: 6));
    print(sameWeekDayOfLastWeek);
    return findLastOfPreviousWeek2(sameWeekDayOfLastWeek);
  }
  
  findLastOfPreviousWeek4(DateTime dateTime) {
    var sameWeekDayOfLastWeek =
    dateTime.subtract(const Duration(days: 6));
    print(sameWeekDayOfLastWeek);
    return findLastOfPreviousWeek3(sameWeekDayOfLastWeek);
  }
  
  findLastOfPreviousWeek5(DateTime dateTime) {
    var sameWeekDayOfLastWeek =
    dateTime.subtract(const Duration(days: 6));
    print(sameWeekDayOfLastWeek);
    return findLastOfPreviousWeek4(sameWeekDayOfLastWeek);
  }
  
  print(DateFormat('dd/MM/yyyy')
    .format(findLastOfPreviousWee2(DateTime.parse('20210922'))));
  
  print(DateFormat('dd/MM/yyyy')
    .format(findLastOfPreviousWee1(DateTime.parse('20210922'))));
  
  print(DateFormat('dd/MM/yyyy')
    .format(findLastOfPreviousWeek(DateTime.parse('20210922'))));
  
  print(DateFormat('dd/MM/yyyy')
    .format(findLastOfPreviousWeek1(DateTime.parse('20210922'))));
  
  print(DateFormat('dd/MM/yyyy')
    .format(findLastOfPreviousWeek2(DateTime.parse('20210922'))));
  
  print(DateFormat('dd/MM/yyyy')
    .format(findLastOfPreviousWeek3(DateTime.parse('20210922'))));
  
  print(DateFormat('dd/MM/yyyy')
    .format(findLastOfPreviousWeek4(DateTime.parse('20210922'))));
  
  print(DateFormat('dd/MM/yyyy')
    .format(findLastOfPreviousWeek5(DateTime.parse('20210922'))));
}

I have run this in dart pad.

I simply want to get the dates of the last 7 Saturdays starting from today's date or less than today's date.

CodePudding user response:

That was fun !

To me there are 2 important elements:

  1. How to get the previous saturday
  2. How to get the x previous saturdays

1. How to get the previous saturday

Here you need to know a bit about modulo, the trick being to find the number of days to subtract from the given date to find the previous saturday:

DateTime findPreviousSaturday({required DateTime from}) {
  // Get the number of days to go back to find a saturday
  final daysFromSaturday = (from.weekday - DateTime.saturday) % 7;

  return from.subtract(Duration(days: daysFromSaturday));
}

How to get the x previous saturdays

Here you could write 7 times the same thing, or apply the first law of programming: be lazy. You can create a recursive function which given a count will return the count previous saturday from the given date. Here is how it work:

  • If count = 0, return an empty list
  • Else, return the previous saturday using findPreviousSaturday and get count - 1 saturday from the previous week

Here is the code:

List<DateTime> findPreviousSaturdays({
  required int count,
  DateTime? from,
}) {
  // End recursion
  if (count == 0) {
    return [];
  }

  // Defaults to now
  final _from = from ?? DateTime.now();

  return [findPreviousSaturday(from: _from)]  
      findPreviousSaturdays(
        count: count - 1,
        from: _from.subtract(Duration(days: 7)),
      );
}

Enjoy

To use it, simply call:

void main() {
  print(findPreviousSaturdays(count: 7));
}

With little to no work you could expand this to work with any days of the week :)

  • Related