コンテンツにスキップ

dart 指定した曜日の直近の日付を取得する#

  DateTime getLastWeekOfDayAndTimeDateTime(int weekOfDay, TimeOfDay timeOfDay) {
    final now = DateTime.now();
    final differenceInDays =
        (DateTime.daysPerWeek + now.weekday - weekOfDay) % DateTime.daysPerWeek;
    final lastWeekOfDayDate = now
        .subtract(Duration(days: differenceInDays != 0 ? differenceInDays : 7));
    return DateTime(lastWeekOfDayDate.year, lastWeekOfDayDate.month,
        lastWeekOfDayDate.day, timeOfDay.hour, timeOfDay.minute);
  }

References#

Tags#

  • #notag