【问题标题】:Flutter: get first day of the week and last day of the week?Flutter:获取一周的第一天和一周的最后一天?
【发布时间】:2021-07-21 21:49:24
【问题描述】:

我想做一些分页来保存同一周的记录。

所以间隔将在周一 00:00 - 周日 23:59 之间

所有这些都在 Unix 时间。所以我可以查询数据库中这个区间的记录。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以使用日期时间。

    DateTime now = DateTime.now();    
    
    int daysOfWeek = now.weekday - 1;
    DateTime firstDay = DateTime(now.year, now.month, now.day - daysOfWeek);
    DateTime lastDay = firstDay.add(Duration(days: 6, hours: 23, minutes: 59));
    
    print(firstDay);
    print(lastDay);
    
    DateTime nextFirst = firstDay.add(Duration(days: 7));
    DateTime nextLast = lastDay.add(Duration(days: 7));
    
    print(nextFirst);
    print(nextLast);
    
    DateTime prevFirst = firstDay.subtract(Duration(days: 7));
    DateTime prevLast = lastDay.subtract(Duration(days: 7));
    
    print(prevFirst);
    print(prevLast);
    

    【讨论】:

      【解决方案2】:

      本周的第一天是

      DateTime firstDayOfCurrentWeek() {
          DateTime now = DateTime.now();
          DateTime firstDayOfCurrentWeek =
              now.subtract(Duration(days: now.weekday - 1));
          return firstDayOfCurrentWeek.clearTime();
        }
      
      
        DateTime clearTime() {
          return DateTime(this.year, this.month, this.day, 0, 0, 0, 0, 0);
        }
      
      

      【讨论】:

        猜你喜欢
        • 2016-07-18
        • 1970-01-01
        • 2013-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-21
        • 1970-01-01
        相关资源
        最近更新 更多