获取Flutter中过去12个月的日期

huangapple go评论56阅读模式
英文:

get last 12 months dates in flutter

问题

我正在创建一个自定义的月份选择器,它会给出过去12个月的列表。

比如现在是8月,它应该返回以下日期列表:

2023年8月、2023年7月、2023年6月、2023年5月......2022年9月

我已经创建了以下的自定义函数,但是不知道如何减去一个月:

List<DateTime> getMonths() {
    
    final dates = <DateTime>[];
    final now = DateTime.now();
    
    int number_of_months = DateTime.now().month + 12;
    
    DateTime date = DateTime.now();
    for(int x = 0; x < number_of_months; x++) {
        dates.add(date);

        // 在这里如何减去一个月?
        date = ??? 
    }
    
    return dates;
}

请帮我解决如何减去一个月的问题。

英文:

I am creating a custom month picker where it gives a list of last 12 months

Like its Auguest so it should return list of dates like

Aug 2023,July 2023,June 2023, May 2023,.........sept 2022

i have created a UDF as following but missing how to deduct a month

List&lt;DateTime&gt; getMonths() {
    
    final dates = &lt;DateTime&gt;[];
    final now = DateTime.now();
    
    int number_of_months=DateTime.now().month+12;
    
    DateTime date=DateTime.now();
    for(int x=0;x&lt;number_of_months;x++)
      {
        dates.add(date);


       date=??? //how to deduct one month here from date
      }
    
    

        return dates;
  }

答案1

得分: 1

要获取一个减去月份的新DateTime,可以按照以下方式操作:

List<DateTime> getLast12Months() {
    List<DateTime> dates = [];
    final today = DateTime.now();
    for (int x = 0; x < 12; x++) {
        dates.add(DateTime(today.year, today.month - x, 1));
    }
    return dates;
}

在上面的代码中,可以看到在for循环中,我们只是从today日期的月份中减去循环值,并创建一个新的DateTime对象。

你将会得到以下输出:

[
   2023-08-01 00:00:00.000, 
   2023-07-01 00:00:00.000, 
   2023-06-01 00:00:00.000, 
   2023-05-01 00:00:00.000, 
   2023-04-01 00:00:00.000, 
   2023-03-01 00:00:00.000, 
   2023-02-01 00:00:00.000, 
   2023-01-01 00:00:00.000, 
   2022-12-01 00:00:00.000, 
   2022-11-01 00:00:00.000, 
   2022-10-01 00:00:00.000, 
   2022-09-01 00:00:00.000
]

你可以使用Intl包根据你的需求格式化日期。

英文:

To get a new DateTime with deducted month, you can do as

List&lt;DateTime&gt; getLast12Months() {
    List&lt;DateTime&gt; dates = [];
    final today = DateTime.now();
    for (int x = 0; x &lt; 12; x++) {
      dates.add(DateTime(today.year, today.month - x, 1));
    }
    return dates;
  }

Here, as you can see in the for loop, we are just deducting the loop value from the month in today date and creating a new DateTime

You will get output as following:

[
   2023-08-01 00:00:00.000, 
   2023-07-01 00:00:00.000, 
   2023-06-01 00:00:00.000, 
   2023-05-01 00:00:00.000, 
   2023-04-01 00:00:00.000, 
   2023-03-01 00:00:00.000, 
   2023-02-01 00:00:00.000, 
   2023-01-01 00:00:00.000, 
   2022-12-01 00:00:00.000, 
   2022-11-01 00:00:00.000, 
   2022-10-01 00:00:00.000, 
   2022-09-01 00:00:00.000
]

You can use Intl package to format the date as per your need.

huangapple
  • 本文由 发表于 2023年8月8日 19:49:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定