【发布时间】:2021-04-13 05:47:30
【问题描述】:
我正在尝试获取从用户创建日期到未来 6 个月的所有月份。假设用户是在 2020 年 10 月创建的,那么我需要从 2020 年 10 月到 2021 年 1 月(当前月份)以及接下来的 6 个月的列表中的所有月份。我在下面尝试过,但输出错误。
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;
}
我的输出是 [2020 年 11 月、2021 年 1 月、2021 年 4 月、2021 年 8 月、2022 年 1 月、2022 年 7 月]。
但我希望我的输出为 [2020 年 10 月、2020 年 11 月、2020 年 12 月、2021 年 1 月、2021 年 2 月、2021 年 3 月、2021 年 4 月、2021 年 5 月、2021 年 6 月] 谁能帮帮我。
【问题讨论】: