c# - Checking if a DateTime value is more than N days old -


i have procedure meant delete items in t-sql database more specified number of days old. starts out like

    [httppost]     public actionresult flushlinks (string numdaysold)     {         // deletes database references links submitted on numdaysold days ago          datetime currentdatetime = datetime.now;         foreach (linkdate thislinkdate in pd.dates)         {             timespan thistimespan = new timespan(convert.toint16(numdaysold), 0, 0, 0);             if ((currentdatetime - thislinkdate.dtime) > thistimespan)             {                 foreach (assetlink thislink in pd.links) if (thislink.linkguid == thislinkdate.linkguid) pd.links.deleteonsubmit(thislink); 

but reason isn't working because when called numdaysold=30 deleted including items recent stamps (2015-06-18 16:36:00, 2015-06-18 16:10:00, etc., of type smalldatetime in database)

is there wrong procedure?

why don't compare dates using datetime.adddays() function, since it's day portion of date you're interested in. here's small example.

class program {     static void main(string[] args)     {         int limitdays = 5;         datetime[] dates =          {             datetime.now.adddays(-1), // falls within 5 days.             datetime.now.adddays(-10) // not fall within 5 days.         };          console.writeline("limit: " + datetime.now.adddays(-limitdays).tostring() + " (" + limitdays.tostring() + " days)");         console.writeline();          foreach (var date in dates)         {             if (datetime.compare(date, datetime.now.adddays(-limitdays)) < 0)             {                 console.writeline("outside limit: " + date.tostring());             }             else if (datetime.compare(date, datetime.now.adddays(-limitdays)) >= 0)             {                 console.writeline("within limit: " + date.tostring());             }         }           console.readline();      } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -