Check DateTime to DateTime.Now

How can I test the use of some form of if if a specific DateTime (say, in this case, called dateAndTime1) is before the current date and time, which I assume will be obtained using DateTime.Now ?

+6
source share
4 answers
 if(dateAndTime1 < DateTime.Now) { //do something } 
+24
source

The operators <, <=,>,> = and == work with DateTime instances, therefore

if(dateAndTime1 < DateTime.Now)

Note that if you are comparing this in a loop, you can get some slight efficiency by setting DateTime now = DateTime.Now before the loop and comparing with now

+3
source
 if(dateTime1 < DateTime.Now){} 
+2
source

Inline works too.

 // bool variable bool isHistory = dateAndTime1 < DateTime.Now; // string return statement return dateAndTime1 < DateTime.Now ? "History" : "Future"; 
+1
source

Source: https://habr.com/ru/post/950576/


All Articles