Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get records in particular date range

Tags:

c#

I have a column in database DateCreated that shows the creation date. Now I want to filter records depending on the date range selected. For example:

  • created within 60 days
  • created within month
  • etc...

I have a variable dateCreated that shows me what the user has selected as the range, i.e. whether it is created within 60 days, created within year, and so on.

        DateTime CurrTime = DateTime.Now;
    if (Program.DateCreated <= DateTime.Now - 60)
    {
        //code to add the record goes here..
    }

But the above code wont work. What would be the syntax to get the records within a particular range?

like image 304
developer Avatar asked Jan 23 '23 04:01

developer


1 Answers

To create a DateTime representing 60 days ago use this:

 DateTime.Now.AddDays(-60)

Note that it would probably be a better idea to send this query to the database rather than filtering on the client.

like image 62
Mark Byers Avatar answered Feb 04 '23 13:02

Mark Byers