[英]date range checking in C#
如何查找輸入中的日期是否在特定日期范圍內(例如wihtin最近7天,這意味着我會說-7)。 如果在最近7天內,請執行其他操作。
我目前可以做到這一點,但是我不知道如何進一步改變以滿足我的需求。
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
首先,使用有意義的名稱而不是a
和b
;其次:使用適當的數據類型(根本不用b
):
int dayOffset = -1;
int lowerBound = -15;
var currentDate = DateTime.Now;
if(input >= currentDate.AddDays(dayOffset) && input <= currentDate)
{ // do smoething }
使用您的名字:
var currentDate = DateTime.Now;
if(input >= currentDate.AddDays(a) && input <= currentDate)
{ // do smoething }
基本上可以使用不到(<)和大於(>)運算符。
我的意思是您應該更改您的if條件:
if (d >= e && d <= DateTime.Now)
您可以嘗試使用類似方法比較沒有Time
Date
部分
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)
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.