如何在颤动列表中获取给定月份的所有日期? [英] How do I get all dates of a given month in a flutter list?

查看:50
本文介绍了如何在颤动列表中获取给定月份的所有日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将给定月份和年份的所有日期添加到动态列表中?

I'd like to know how I could get all dates of a given month and year added into a Dynamic List in flutter?

想法是将这些信息显示在数据表中.

The idea is to display this information in a data table.

如何将所有日期(给定月份)放入列表中?

How can I put all dates (for a given month) in a List?

List myListOfDates = Dates(may2020).format(day.month.year)

我该如何生产?

谢谢

推荐答案

如果我正确理解您的情况,我规定的是查找指定月份的所有日期.

If I understand your situation right, what I have stipulated is to find all the dates of the particular specified month.

算法

  1. 以月份数字和年份作为输入,并将其传递给DateTime()
  2. 查找所提供的当月的总天数
  3. 生成所有日期,直到从步骤1获得总天数为止
  4. 打印以检查

在继续执行代码之前,您可能需要检查一下,这将帮助您更好地理解情况:

Before we proceed to the code, you might want to check this out, this will help you understand the situation better and clear:

代码:它不需要任何导入,因此请遵循.

CODE: It doesn't require any imports, so follow along.

void main() {
  // Take the input year, month number, and pass it inside DateTime()
  var now = DateTime(2020, 7);
  
  // Getting the total number of days of the month
  var totalDays = daysInMonth(now);
  
  // Stroing all the dates till the last date
  // since we have found the last date using generate
  var listOfDates = new List<int>.generate(totalDays, (i) => i + 1);
  print(listOfDates);
}

// this returns the last date of the month using DateTime
int daysInMonth(DateTime date){
  var firstDayThisMonth = new DateTime(date.year, date.month, date.day);
  var firstDayNextMonth = new DateTime(firstDayThisMonth.year, firstDayThisMonth.month + 1, firstDayThisMonth.day);
  return firstDayNextMonth.difference(firstDayThisMonth).inDays;
}

输出

// since we used month 7, in the DateTime(), so it returned 31, which will give output
// till last date of the specified month
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]

改进

如果要以这种格式存储数据,请 dd/mm/yyyy .我们可以随时对其进行修改.可以这样做,对代码进行一些改进

If you want to store the data in this format, dd/mm/yyyy. We can always modify it. It can be done like this, with a little improvements in the code

// make sure you define you List<String> not List<int> in the previous code
// also, in place of May and 2020, you can add your input params for month and year, make sure to convert the numeric month to word format like 7 => July
// like "${i+1} $month, $year"
// I have used my words only
var listOfDates = new List<String>.generate(lastDateOfMonth, (i) => "${i+1}/July/2020");
print(listOfDates);

您还可以按照自己喜欢的任何形式存储数据,我喜欢日期/月/年

You can also, store the data in whatever form you like, I liked date/month/year

输出

[1/July/2020, 2/July/2020, 3/July/2020, 4/July/2020, 5/July/2020, 6/July/2020, 7/July/2020, 8/July/2020, 9/July/2020, 10/July/2020, 11/July/2020, 12/July/2020, 13/July/2020, 14/July/2020, 15/July/2020, 16/July/2020, 17/July/2020, 18/July/2020, 19/July/2020, 20/July/2020, 21/July/2020, 22/July/2020, 23/July/2020, 24/July/2020, 25/July/2020, 26/July/2020, 27/July/2020, 28/July/2020, 29/July/2020, 30/July/2020, 31/July/2020]

这篇关于如何在颤动列表中获取给定月份的所有日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆