I am trying to get all the months from user created date to future 6 months. Suppose user is created in October 2020 then I need all the months in list from October 2020 to January 2021(current month) plus next 6 months. I tried below but getting wrong output.
static List<String> getMonthsInYear(){
List<String> years=[];
DateFormat dateFormat=DateFormat("MMM yyyy");
DateTime createdDate = DateTime(2020,10,1,0,0,0,0,0);
DateTime today = DateTime.now();
var i=0,currentMonth=1;
while(today.year==createdDate.year && today.month==createdDate.month){
createdDate=DateTime(createdDate.year,createdDate.month+i,1,0,0,0,0,0);
years.add(dateFormat.format(createdDate));
i++;
}
while(currentMonth<7){
createdDate=DateTime(createdDate.year,createdDate.month+currentMonth,1,0,0,0,0,0);
years.add(dateFormat.format(createdDate));
currentMonth++;
}
print("BLB createdDate ${years}");
return years;
}
My output is [Nov 2020, Jan 2021, Apr 2021, Aug 2021, Jan 2022, Jul 2022].
But I wanted my output to be [Oct 2020, Nov 2020, Dec 2020, Jan 2021, Feb 2021, Mar 2021, Apr 2021, May 2021, Jun 2021] Can someone help me.