【问题标题】:How to get month names from created date to future 6 months in flutter如何在颤动中获取从创建日期到未来 6 个月的月份名称
【发布时间】: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 月] 谁能帮帮我。

【问题讨论】:

    标签: flutter datetime dart


    【解决方案1】:

    这应该给你你想要的:

    static List<String> getMonthsInYear(DateTime userCreatedDate) {
        final dates = <String>[];
        final now = DateTime.now();
        final sixMonthFromNow = DateTime(now.year, now.month+6);
        DateTime date = userCreatedDate;
    
        while (date.isBefore(sixMonthFromNow)) {
          dates.add(dateFormat.format(date));
          date = DateTime(date.year, date.month+1);
        }
        return dates;
      }
    }
    

    【讨论】:

      【解决方案2】:

      这应该给你你想要的:

        static List<String> getMonthsInYear(DateTime userCreatedDate) {
          DateFormat dateFormat=DateFormat("MMM yyyy");
      
          return List.generate(6, (int index) {
            final date = DateTime(userCreatedDate.year, userCreatedDate.month + index);
            return dateFormat.format(date);
          });
        }
      

      【讨论】:

      • 感谢您的回答。我已经编辑了我的问题。对不起。我需要创建月份到当前月份,然后是当前月份的下一个 6 个月。
      • @BLB 明白了。让我看看我能不能想出点什么来。
      【解决方案3】:

      我不知道为什么这是你未来 6 个月的产出。

      [2020 年 10 月、2020 年 11 月、2020 年 12 月、2021 年 1 月、2021 年 2 月、2021 年 3 月、2021 年 4 月、2021 年 5 月、2021 年 6 月]

      如果你想要的输出是这样的

      [2020 年 10 月、2020 年 11 月、2020 年 12 月、2021 年 1 月、2021 年 2 月、2021 年 3 月]

      代码如下所示:

        static List<String> getMonthsInYear(DateTime createdDate, int length){
          DateFormat dateFormat=DateFormat("MMM yyyy");
      
          List<String> years=[];
      
          int currentYear = createdDate.year;
          int currentMonth = createdDate.month;
          for(int i=0; i<length; i++) {
            createdDate=DateTime(currentYear, currentMonth + i);
            years.add(dateFormat.format(createdDate));
      
            if(currentMonth + i == 1) {
              currentYear += 1;
            }
          }
          return years;
        }
      
      

      使用代码:

      DateTime createdDate = DateTime(2020,10,1,0,0,0,0,0);
      print(getMonthsInYear(createdDate, 6));
      

      【讨论】:

      • 感谢您的回答。我已经编辑了我的问题。对不起。我需要创建月份到当前月份,然后是当前月份的下一个 6 个月
      • 我将长度作为我编写的 getMonthsInYear 函数的第二个参数。你可以为所欲为。
      猜你喜欢
      • 2010-12-11
      • 1970-01-01
      • 2020-06-17
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多