Given a date, and let's say it is Monday, I hope that if I add 7 days I will get a new Monday.
That is so sometimes, in some case it adds a little less and I get a Sunday. I can keep adding 7 days and I get Sundays until I get a new Monday back.
How can I make a precise cycle that returns me the same day of the week.
Here I leave the code in Dart. Linked to a dartpad
void main() {
var date = DateTime(2020, 10, 20);
var weekday = date.weekday;
var endDate = DateTime(2021, 4, 8);
while (endDate.difference(date).inDays > 0) {
assert(date.weekday == weekday);
print('$date ${date.weekday} $weekday');
date = date.add(Duration(days: 7));
}
print('end');
}
The result is
2020-10-20 00:00:00.000 2 2
2020-10-26 23:00:00.000 1 2
2020-11-02 23:00:00.000 1 2
2020-11-09 23:00:00.000 1 2
2020-11-16 23:00:00.000 1 2
2020-11-23 23:00:00.000 1 2
2020-11-30 23:00:00.000 1 2
2020-12-07 23:00:00.000 1 2
2020-12-14 23:00:00.000 1 2
2020-12-21 23:00:00.000 1 2
2020-12-28 23:00:00.000 1 2
2021-01-04 23:00:00.000 1 2
2021-01-11 23:00:00.000 1 2
2021-01-18 23:00:00.000 1 2
2021-01-25 23:00:00.000 1 2
2021-02-01 23:00:00.000 1 2
2021-02-08 23:00:00.000 1 2
2021-02-15 23:00:00.000 1 2
2021-02-22 23:00:00.000 1 2
2021-03-01 23:00:00.000 1 2
2021-03-08 23:00:00.000 1 2
2021-03-15 23:00:00.000 1 2
2021-03-22 23:00:00.000 1 2
2021-03-30 00:00:00.000 2 2
2021-04-06 00:00:00.000 2 2
end