Home > Software engineering >  Get DateTime by loop with 2 months gap
Get DateTime by loop with 2 months gap

Time:04-20

Hi guys i would like some help how can i achieve to get the date with a gap of 2 months or more so far i did on the code below

final dateNow  = DateTime.now();

DateTime biMonth(DateTime? timeDate, int? add){
 DateTime result = DateTime(dateNow.year,dateNow.month  add! , dateNow.day);
 return result;
}


 /// The Function
  getDatesGaps(){
    DateTime timeNow = DateTime.now();
    for(int p = 1 ; p < 3 ; p  ){
       timeNow = biMonth(timeNow,p);
      log(timeNow.toString());
    }

  }

the result i get

2022-06-19 15:55:00.000
2022-07-19 15:55:00.000

result i want is like this

2022-06-19 15:55:00.000
2022-08-19 15:55:00.000

have a gap with 2 months.

CodePudding user response:

You could just do

timeNow = biMonth(timeNow,p * 2);
  • Related