Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if datetime variable is today, tomorrow or yesterday

Tags:

datetime

dart

I do not know how to check if datetime variable is today, tomorrow or yesterday.

I did not find a method in the class members.

like image 970
Giacomo M Avatar asked Jan 27 '19 18:01

Giacomo M


People also ask

How can I check yesterday's date today?

To check if a date is yesterday:Subtract 1 day from the current date to get yesterday's date.

How can I get tomorrow date in C#?

Explanation: In the above example, first of all, we find tomorrow's date by adding today's date with the timespan of 1. Here, we get today's date using the DateTime. Now method.

How can I get yesterday in PHP today?

Description: To get yesterday's date in PHP simply use the DateTime class and instantiate it with the constructor parameter 'yesterday' Then you can format the DateTime object with the format method.


1 Answers

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.

like image 50
Günter Zöchbauer Avatar answered Sep 29 '22 11:09

Günter Zöchbauer