Flutter:获取一周的第一天和一周的最后一天?

时间:2021-04-28 14:11:08

标签: flutter dart

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

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

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

2 个答案:

答案 0 :(得分:0)

您可以使用日期时间。

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);

答案 1 :(得分:0)

本周第一天是

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);
  }