Get records in a specific date range

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

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

I have a DateCreated variable that shows me what the user has selected as a range, i.e. whether it was created within 60 days created during the year, etc.

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

But the above code will not work. What will be the syntax for getting records in a certain range?

+4
source share
3 answers

To create a DateTime representing 60 days ago, use this:

  DateTime.Now.AddDays(-60) 

Please note that it would be better to send this request to the database rather than to the client.

+6
source

Something like that?

if (dateCreated >= DateTime.Now.Subtract(myTimeSpanRange))

where myTimeSpanRange is a TimeSpan indicating how far back the user wants to go.

+2
source

To create TimeSpan Matt, you can use this:

 if (Program.DateCreated <= dateCreated - TimeSpan.FromDays(60)) { ... } 
+1
source

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


All Articles