簡體   English   中英

您好,我需要比較 2 個日期,例如開始日期和結束日期,如果我 select 是星期三,我需要這些日期之間的所有星期三

[英]hello i need to compare 2 dates like start date and end date and if i select a wednesday i need all wednesdays occuring between these dates

我有一個場景,用戶選擇開始日期和結束日期,用戶還選擇一個特定的日期,我需要顯示該特定日期和它們之間的日期。

我嘗試了 Intl package 差異方法但沒有用

你可以使用這個方法。 采用開始和結束日期以及工作日。 注意,可以將 3 作為 int 或 'DateTime.wednesday' 作為參數傳入。

注意,基於 mirkancal 在此線程中的回答的想法


List<DateTime> getAllDatesOfAWeekday(
    {required DateTime startDate,
    required DateTime endDate,
    required int weekday}) {
  List<DateTime> allDates = [];

  for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
    if (startDate.add(Duration(days: i)).weekday == weekday) {
      allDates.add(startDate.add(Duration(days: i)));
    }
  }
  return allDates;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM