How to find if a date in the input is within a specific daterange (let's say for example wihtin last 7 days, meaning I will say -7). If it is within last 7 days, do something, else do something else.
I currently could do upto this, but I don't know how to change this further to meet what I want.
string a = "-1"; // These are values that are configurable based on which date is checked. Yesterday means, -1 for example.
string b = "-15"; // -15 means within last 15 days.
DateTime d = input;
DateTime e = d.AddDays(int.Parse(a));
if (d is between datetime.now and e)
{
//do something
}
else do something
First of all, use meaningful names instead of a
and b
, secondly: use proper data types (you don't use b
at all):
int dayOffset = -1;
int lowerBound = -15;
var currentDate = DateTime.Now;
if(input >= currentDate.AddDays(dayOffset) && input <= currentDate)
{ // do smoething }
Using your names:
var currentDate = DateTime.Now;
if(input >= currentDate.AddDays(a) && input <= currentDate)
{ // do smoething }
You can use less than(<) and greater than(>) operator basicly.
I mean you should change your if conditionlike:
if (d >= e && d <= DateTime.Now)
You can try something like this to compare Date
portion without Time
string a = "-1"; // These are values that are configurable based on which date is checked. Yesterday means, -1 for example.
string b = "-15"; // -15 means within last 15 days.
DateTime d = new DateTime();
DateTime e = d.AddDays(int.Parse(a));
if (DateTime.Now.Date >= d.Date && e.Date <= d.Date)
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.