Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if date x are older than some number of days

Tags:

c#

datetime

This is my first project in C#. I need Check if date x are older than some number of days.

Example:

if ( THIS_IS_SAVED_DATE_5_DAYS_AGO < OLDER_THAN_5_DAYS ) {

// Do this if saved date is more than 5 days

} 

Thanks for help.

like image 735
Marcin Nowak Avatar asked Mar 11 '17 09:03

Marcin Nowak


2 Answers

You could use the AddDays method to construct a new date relative to the current date and then compare the two dates:

DateTime x = ...
if (x < DateTime.Now.AddDays(-5)) 
{
    // x is older than 5 days
}
like image 149
Darin Dimitrov Avatar answered Nov 17 '22 13:11

Darin Dimitrov


You can do something like:

if (x < x.AddDays(-1*days)) {

}
like image 4
dcg Avatar answered Nov 17 '22 15:11

dcg