如何使用DateTime每隔一周计算一次



我有聊天应用程序。我想只允许用户每隔一周给其他用户发一条消息。例如:

  • 第1周(允许
  • 第2周(不允许(
  • 第3周(允许(
  • 第4周(不允许(

我该怎么做?我想写有条件的,所以可以说:

if (allowedWeek) {
sendMessage
}

也许可以用这样的东西?

DateTime.now().isAfter(X) && DateTime.now().isBefore(Y)

我如何告诉DateTime这个?

也许你可以通过这个包获得一年中的一周,week_of_year,并决定谈论哪一周不是:

import 'package:week_of_year/week_of_year.dart';
void main() {
final date = DateTime.now();
print(date.weekOfYear); // Get the iso week of year
print(date.ordinalDate); // Get the ordinal date
print(date.isLeapYear); // Is this a leap year?
}

相关内容

最新更新