How to check a date is expired or not in Flutter?

Viewed 2757

How can i check if a date is expired or not. for eg: if the expire date is 10/01/2021, if the user check today(09/01/2021) it should print "Not Expired", if the user check on 11/01/2021 or after the expiry days it should print "the date is expired". How can i implement this in flutter.

1 Answers

DateTime type has handy method isBefore and isAfter to compare two DateTime objects.

For example:

final now = DateTime.now();
final expirationDate = DateTime(2021, 1, 10);
final bool isExpired = expirationDate.isBefore(now);

In this example, isExpired variable will be true if it has passed the expirationDate and false if it has not passed the expiration date.

Related