Flutter 如何在dart逻辑中获取每周日期列表

Flutter 如何在dart逻辑中获取每周日期列表,flutter,dart,Flutter,Dart,有谁能帮我得到一个月内每周的日期列表,并选择工作日的开始日期,如星期一或任何其他日期 无法理解颤振(Dart)中的逻辑 例如: 输入: 月份:5月 工作日:星期一 输出: [ { "week1": [ "-", "-", "-", "-", "2020-05-01", "2020-05-02", "2020-05-03" ] }, { "week2": [ "202

有谁能帮我得到一个月内每周的日期列表,并选择工作日的开始日期,如星期一或任何其他日期

无法理解颤振(Dart)中的逻辑

例如:

输入:
月份:5月
工作日:星期一

输出:

[
  {
    "week1": [
      "-",
      "-",
      "-",
      "-",
      "2020-05-01",
      "2020-05-02",
      "2020-05-03"
    ]
  },
  {
    "week2": [
      "2020-05-04",
      "2020-05-05",
      "2020-05-06",
      "2020-05-07",
      "2020-05-08",
      "2020-05-09",
      "2020-05-10"
    ]
  },
  {
    "week3": [
      "2020-05-11",
      "2020-05-12",
      "2020-05-13",
      "2020-05-14",
      "2020-05-15",
      "2020-05-16",
      "2020-05-17"
    ]
  },
  {
    "week4": [
      "2020-05-18",
      "2020-05-19",
      "2020-05-20",
      "2020-05-21",
      "2020-05-22",
      "2020-05-23",
      "2020-05-24"
    ]
  },
  {
    "week5": [
      "2020-05-25",
      "2020-05-26",
      "2020-05-27",
      "2020-05-28",
      "2020-05-29",
      "2020-05-30",
      "2020-05-31"
    ]
  }
]

我终于找到了解决办法。在此处发布以供将来其他人使用

  calculateWeeks(DateTime currentMonth, DateTime previousMonth) {
  List<List<String>> weeks = [];
  currentMonth = DateTime.utc(2020, DateTime.august, 1);
  if (currentMonth == null) {
    currentMonth = DateTime.now();
  }

  int firstDay = 1;
  int lastDay = DateUtils.daysofMonth(currentMonth);
  log('Day from $firstDay - $lastDay');
  DateTime startMonth =
      DateTime.utc(currentMonth.year, currentMonth.month, firstDay);
  DateTime endMonth =
      DateTime.utc(currentMonth.year, currentMonth.month, lastDay);

  DateTime weekStartDates = DateUtils.weekStart(startMonth);
  DateTime weekEndDates = DateUtils.weekEnd(endMonth);
  int daysDiff = weekEndDates.difference(weekStartDates).inDays;
  log('Start week : $weekStartDates');
  log('End   week : $weekEndDates');
  log('Diff       : ${daysDiff}');
  List<String> weekly = [];
  for (int i = 0; i < daysDiff; i++) {
    String date = '';
    DateTime checkdate = weekStartDates.add(Duration(days: i));
    if ((checkdate == startMonth || checkdate.isAfter(startMonth)) &&
        (checkdate == endMonth || checkdate.isBefore(endMonth))) {
      log('weekday : $i - $checkdate : ${checkdate.weekday}');
      date = checkdate.toIso8601String();
    }
    weekly.add(date);
    if (checkdate.weekday == 7 || i == daysDiff - 1) {
      weeks.add(weekly);
      weekly = [];
    }
  }
  log('Weekly Dates :${json.encode(weeks)}');
}
CalculateWeks(DateTime currentMonth,DateTime previousMonth){
列表周=[];
currentmount=DateTime.utc(2020年,DateTime.8月1日);
如果(currentMonth==null){
currentmount=DateTime.now();
}
int firstDay=1;
int lastDay=DateUtils.daysofMonth(currentMonth);
日志(“从$firstDay到$lastDay的日期”);
日期时间开始月=
DateTime.utc(currentmount.year、currentmount.month、firstDay);
日期时间月末=
DateTime.utc(currentmount.year、currentmount.month、lastDay);
DateTime weekStartDates=DateUtils.weekStart(startMonth);
DateTime weekEndDates=DateUtils.weekEnd(endMonth);
int daysDiff=weekEndDates.difference(weekStartDates).inDays;
日志(“开始周:$weekStartDates”);
日志(“周末:$weekEndDates”);
日志('Diff:${daysDiff}');
每周列表=[];
对于(int i=0;i
我终于找到了解决方案。在此处发布以供将来其他人使用

  calculateWeeks(DateTime currentMonth, DateTime previousMonth) {
  List<List<String>> weeks = [];
  currentMonth = DateTime.utc(2020, DateTime.august, 1);
  if (currentMonth == null) {
    currentMonth = DateTime.now();
  }

  int firstDay = 1;
  int lastDay = DateUtils.daysofMonth(currentMonth);
  log('Day from $firstDay - $lastDay');
  DateTime startMonth =
      DateTime.utc(currentMonth.year, currentMonth.month, firstDay);
  DateTime endMonth =
      DateTime.utc(currentMonth.year, currentMonth.month, lastDay);

  DateTime weekStartDates = DateUtils.weekStart(startMonth);
  DateTime weekEndDates = DateUtils.weekEnd(endMonth);
  int daysDiff = weekEndDates.difference(weekStartDates).inDays;
  log('Start week : $weekStartDates');
  log('End   week : $weekEndDates');
  log('Diff       : ${daysDiff}');
  List<String> weekly = [];
  for (int i = 0; i < daysDiff; i++) {
    String date = '';
    DateTime checkdate = weekStartDates.add(Duration(days: i));
    if ((checkdate == startMonth || checkdate.isAfter(startMonth)) &&
        (checkdate == endMonth || checkdate.isBefore(endMonth))) {
      log('weekday : $i - $checkdate : ${checkdate.weekday}');
      date = checkdate.toIso8601String();
    }
    weekly.add(date);
    if (checkdate.weekday == 7 || i == daysDiff - 1) {
      weeks.add(weekly);
      weekly = [];
    }
  }
  log('Weekly Dates :${json.encode(weeks)}');
}
CalculateWeks(DateTime currentMonth,DateTime previousMonth){
列表周=[];
currentmount=DateTime.utc(2020年,DateTime.8月1日);
如果(currentMonth==null){
currentmount=DateTime.now();
}
int firstDay=1;
int lastDay=DateUtils.daysofMonth(currentMonth);
日志(“从$firstDay到$lastDay的日期”);
日期时间开始月=
DateTime.utc(currentmount.year、currentmount.month、firstDay);
日期时间月末=
DateTime.utc(currentmount.year、currentmount.month、lastDay);
DateTime weekStartDates=DateUtils.weekStart(startMonth);
DateTime weekEndDates=DateUtils.weekEnd(endMonth);
int daysDiff=weekEndDates.difference(weekStartDates).inDays;
日志(“开始周:$weekStartDates”);
日志(“周末:$weekEndDates”);
日志('Diff:${daysDiff}');
每周列表=[];
对于(int i=0;i
您是否使用日期选择器?你想不包括周末吗?“问题”的措辞有点混乱,您想让代码获得正确的输出吗?你没试过什么吗?谢谢你的回答,我自己找到了解决方案。你在用日期选择器吗?你想不包括周末吗?“问题”的措辞有点混乱,您想让代码获得正确的输出吗?你没试过什么吗?谢谢你的回答,我自己找到了解决办法