如何在 Flutter 中检查日期是否已过期?
How to check a date is expired or not in Flutter?
如何检查日期是否已过期。例如:如果过期日期是 10/01/2021,如果用户今天检查(09/01/2021),它应该打印“未过期”,如果用户在 11/01/2021 或过期天数之后检查它应该打印“日期已过期”。我如何在 flutter 中实现它。
DateTime
类型有方便的方法 isBefore
和 isAfter
来比较两个 DateTime
对象。
例如:
final now = DateTime.now();
final expirationDate = DateTime(2021, 1, 10);
final bool isExpired = expirationDate.isBefore(now);
在此示例中,isExpired
变量如果已过期则为 true
,如果未过期则为 false
。
如何检查日期是否已过期。例如:如果过期日期是 10/01/2021,如果用户今天检查(09/01/2021),它应该打印“未过期”,如果用户在 11/01/2021 或过期天数之后检查它应该打印“日期已过期”。我如何在 flutter 中实现它。
DateTime
类型有方便的方法 isBefore
和 isAfter
来比较两个 DateTime
对象。
例如:
final now = DateTime.now();
final expirationDate = DateTime(2021, 1, 10);
final bool isExpired = expirationDate.isBefore(now);
在此示例中,isExpired
变量如果已过期则为 true
,如果未过期则为 false
。