check if datetime variable is today, tomorrow or yesterday

final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final yesterday = DateTime(now.year, now.month, now.day - 1);
final tomorrow = DateTime(now.year, now.month, now.day + 1);


final dateToCheck = ...
final aDate = DateTime(dateToCheck.year, dateToCheck.month, dateToCheck.day);
if(aDate == today) {
  ...
} else if(aDate == yesterday) {
  ...
} else(aDate == tomorrow) {
  ...
}

Hit: now.day - 1 and now.day + 1 works well with dates that result in a different year or month.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)