Reputation: 5478
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:
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?
Upvotes: 4
Views: 991
Reputation: 516
To generate the TimeSpan Matt suggests, you could use this:
if (Program.DateCreated <= dateCreated - TimeSpan.FromDays(60)) {
...
}
Upvotes: 1
Reputation: 838106
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.
Upvotes: 6
Reputation: 62027
Something like this?
if (dateCreated >= DateTime.Now.Subtract(myTimeSpanRange))
where myTimeSpanRange is a TimeSpan
indicating how far back the user wants to go.
Upvotes: 2