Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 如何在颤振列表中获取给定月份的所有日期?_Date_Flutter_Dart - Fatal编程技术网

Date 如何在颤振列表中获取给定月份的所有日期?

Date 如何在颤振列表中获取给定月份的所有日期?,date,flutter,dart,Date,Flutter,Dart,我想知道如何将给定月份和年份的所有日期添加到Flitter中的动态列表中 其思想是在数据表中显示此信息 如何在列表中列出给定月份的所有日期 List myListOfDates = Dates(may2020).format(day.month.year) 我如何制作这个 谢谢如果我正确理解您的情况,我规定的是查找特定月份的所有日期 算法 以月份和年份为单位输入,并在DateTime内传递 查找所提供月份的总天数 生成所有日期,直到我们从步骤1获得的总天数 打印检查 在我们继续编写代码之前,您

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

其思想是在数据表中显示此信息

如何在列表中列出给定月份的所有日期

List myListOfDates = Dates(may2020).format(day.month.year)
我如何制作这个


谢谢

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

算法

以月份和年份为单位输入,并在DateTime内传递 查找所提供月份的总天数 生成所有日期,直到我们从步骤1获得的总天数 打印检查 在我们继续编写代码之前,您可能想检查一下,这将帮助您更好地了解情况:

代码:它不需要任何导入,所以请继续

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;
}
改进

如果要以这种格式存储数据,请选择dd/mm/yyyy。我们可以随时修改它。可以这样做,只需对代码稍加改进

// 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);
首先,我们需要这个包来获得一个月的天数

其次,我们需要这个包来获取DateTime格式

您可以通过以下代码进行尝试:

导入“package:date_util/date_util.dart”

导入“包:intl/intl.dart”

您可以将DateFormat.YEAR\u MONTH\u DAY更改为您需要的任何内容

这是输出的图像


非常感谢你。你的最后一句话一针见血:快乐学习。我和你的水平不一样,我也不确定我是否完全理解你的代码。。但我会和伊萨哈一起玩!嘿@houba,永远不要考虑等级和所有的东西。每个人都以自己的速度工作,并达到顶峰。我也一样,只是一个学习者,也在通过这个学习。玩它。你也会在那里。如果你觉得这很有用,我很高兴看到它被升级并标记为正确。再次愉快地学习,一切都很好:另外,看看我的答案中新增的部分,那就是改进。这将对您有用:我得到了解决方案,可以使用vars。但是我给这个100%的分数是因为a教会了我如何思考,b date_utils和dart 2.8.X是不兼容的Hanks@houba,我真的很高兴看到你学习!祝你一切顺利甚至我也通过你了解到了这种不相容性,非常感谢。非常感谢。这将是完美的,但它对我来说不起作用,因为显然这个pub.dev只对dart<2.8.x有效
// 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);
     @override
     void initState() {
      final daysCount = DateUtil().daysInMonth(DateTime.may, 2020);
      List<String> days = [];
      for (int i = 1; i < daysCount+1; i++) {
      days.add(DateFormat(DateFormat.YEAR_MONTH_DAY)
          .format(DateTime(2020, DateTime.may, i)));
      }
     super.initState();
     }