Home > Mobile >  How to get dates by days which have intervals of 7 days each
How to get dates by days which have intervals of 7 days each

Time:04-19

hello guyz am new to flutter and am practicing on dates then i encounter a difficulty on how can i acheive the result same below using a loop or something in order to get this result.

so far i tried is

final now = DateTime.now();

for(var d = 1 ; d <= 5 ; d  ){
  // Don't know whats next to do here to get the same result below
}

i want result like this

Apr 19, 2022
Apr 26, 2022
May  3, 2022
May 10, 2022
May 17, 2022

can some help me and explain.

CodePudding user response:

Dart's DateTime class has an add function, where you can specify a duration which will be added the current Date.

Therefore, what you can do is:

DateTime now = DateTime.now();

for(var d = 1; d <= 5; d  ) {
  ...
  now = now.add(Duration(days: 7)); // here you can specify your interval. Also possible: hours, minutes, seconds, milliseconds and microseconds
}

I'll recommend reading the Docs, e.g. DateTime Docs and Duration Docs

  • Related