[英]Filter or Check Date in A Date Range
這完全是一個簡單或基本的要求。 我正在嘗試使用C#
從日期列表中獲取日期。 因此,我完成了一個函數,並使用for
循環對其進行了迭代。 我試圖將列表函數分為兩個范圍,並通過DatePicker
控件傳遞值,如下所示:
private void btnClick_Click(object sender, EventArgs e)
{
DateTime theFromDate = dateTimePicker1.Value;
DateTime theToDate = dateTimePicker2.Value;
List<DateRange> lstRange = GetDateRange();
/**Trying To Get The Date From The Range - Starts**/
var dates = new List<DateTime>();
for (var dt = theFromDate; dt <= theToDate; dt = dt.AddDays(1))
{
dates.Add(dt);
//MessageBox.Show(dt.Date.ToString());
}
List<DateRange> lst = GetDateRange();
foreach(var item in lst)
{
if(theFromDate <= item.EndtDate.Date)
{
MessageBox.Show(theFromDate.ToString("dd-MMM-yyyy") + " in the date range!");
}
else
{
MessageBox.Show(theFromDate.ToString("dd-MMM-yyyy") + " not in the date range!");
}
}
/**Trying To Get The Date From The Range - Ends**/
}
public class DateRange
{
public DateTime date { set; get; }
public DateTime EndtDate { set; get; }
}
/**List of Dates Here - Starts**/
public List<DateRange> GetDateRange()
{
List<DateRange> lstDate = new List<DateRange>();
DateRange aDateRange = new DateRange();
aDateRange.StartDate = Convert.ToDateTime("10-Aug-2018");
aDateRange.EndtDate = Convert.ToDateTime("13-Aug-2018");
lstDate.Add(aDateRange);
return lstDate;
}
/**List of Dates Here - Ends**/
不幸的是,盡管列表有特定的日期,但是這沒有返回期望的輸出。
更新1: 預期輸出 FromDate
和ToDate
值存儲在列表中。
FromDate ToDate
10-AUG-2018 13-AUG-2018
**in the date range**
FromDate ToDate
13-AUG-2018 16-AUG-2018
**in the date range** //As 13 is the end date in the given list
FromDate ToDate
8-AUG-2018 10-AUG-2018
**in the date range** //As 10 is the start date in the given list
FromDate ToDate
8-AUG-2018 8-AUG-2018
**not in the date range** //As 10 is the start date in the given list
老實說,我在弄清楚您要做什么時遇到了一些麻煩,我不禁感到您在“過度設計”您的解決方案。
首先,“日期范圍”只是兩個日期-凝視日期和結束日期,但是您的GetDateRange
方法內部有4個日期,它將作為列表返回。 這令人難以置信地令人困惑-而且我不確定您是要獲取多個日期范圍(多個對)還是單個日期范圍。 鑒於所有的日期都是一個接一個的,我將假設后者。
public class DateRange
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public DateRange GetStaticDateRange()
{
//It seems counterproductive to add all 4 dates here,
//given that these are all one after the other
return new DateRange
{
StartDate = new DateTime(2018, 7, 10),
EndDate = new DateTime(2018, 7, 13)
};
//Obviously this can be modified as needed to return whatever combination of
//start-end dates you want, but this method will only ever return ONE range
//However, this method could just as well accept parameters and / or access other resources
}
public bool IsInDateRange(DateTime dateToCheck, DateRange targetRange)
{
//An argument can be made to use non-encompassing comparisons for both checks
//depending on your requirements
return dateToCheck >= targetRange.StartDate && dateToCheck <= targetRange.EndDate;
}
上面有一個用於存儲“ date-range”(適當地稱為DateRange
)的簡單類,以及一個示例方法,該方法檢查給定的DateTime
在特定DateRange
內部是否有效。
編輯:
好的,所以從更新的問題看來,您似乎正在嘗試查找兩個日期范圍是否完全重疊。
在這種情況下,下面的代碼應該會有所幫助。
public static bool DateRangesOverlap(DateRange range1, DateRange range2)
{
return (range1.StartDate >= range2.StartDate && range1.StartDate <= range2.EndDate) ||
(range1.EndDate >= range2.StartDate && range1.EndDate <= range2.EndDate);
}
這是您的測試用例在.NET Fiddle上的工作示例。 請注意,我仍然使用上面定義的DateRange
類,並添加了簡短的構造函數。
還請注意, DateRange
類沒有對start和end參數進行完整性檢查,並且可以創建一個DateRange
並反轉兩個值(即start> end),這顯然會導致錯誤。 但這只是一個示例,因此我將這些事情的實現留給您。 ;)
您的代碼中有一些錯誤。 例如,在GetDateRange()中,您只向該范圍添加一個日期,其日期將設置為2018年8月13日,因此這是您需要修復的一件事,並且如果您的目標是在以下位置查找日期:一定日期范圍內,您可以使用Linq。 為了比較范圍,我還建議使用DateTime.CompareTo方法。 請參見下面的代碼以更正您的錯誤:
public static bool RangeContainsDate(DateTime queriedDateTime)
{
var queriedDateRange = new DateRange { Date = queriedDateTime };
List<DateRange> dates = GetDateRange();
return dates.Where(d => d.CompareTo(queriedDateRange) == 0).Any();
}
/**List of Dates Here - Starts**/
public static List<DateRange> GetDateRange()
{
List<DateRange> lstDate = new List<DateRange>();
DateRange aDateRange1 = new DateRange();
aDateRange1.Date = Convert.ToDateTime("10-Aug-2018");
lstDate.Add(aDateRange1);
DateRange aDateRange2 = new DateRange();
aDateRange2.Date = Convert.ToDateTime("11-Aug-2018");
lstDate.Add(aDateRange2);
DateRange aDateRange3 = new DateRange();
aDateRange3.Date = Convert.ToDateTime("12-Aug-2018");
lstDate.Add(aDateRange3);
DateRange aDateRange4 = new DateRange();
aDateRange4.Date = Convert.ToDateTime("13-Aug-2018");
lstDate.Add(aDateRange4);
return lstDate;
}
}
}
public class DateRange : IComparable<DateRange>
{
public DateTime Date { set; get; }
public int CompareTo(DateRange other)
{
if (ReferenceEquals(other, null))
{
return -1;
}
return DateTime.Compare(Date, other.Date);
}
}
private void btnClick_Click(object sender, EventArgs e)
{
//DateTime theFromDate = dateTimePicker1.Value;
DateTime theToDate = dateTimePicker2.Value;
List<DateRange> lstRange1 = GetDateRange();
List<DateRange> lstRange2 = GetDateRange();
var result = lstRange1.Any(x => x.date >= theToDate && lstRange2.Any(y => y.date < theToDate));
if (result)
{
MessageBox.Show(theToDate.ToString("dd-MMM-yyyy") + " in the date range!");
}
else
{
MessageBox.Show(theToDate.ToString("dd-MMM-yyyy") + " not in the date range!");
}
}
public List<DateRange> GetDateRange()
{
List<DateRange> lstDate = new List<DateRange>();
lstDate.Add(new DateRange { date = Convert.ToDateTime("10-Aug-2018") });
lstDate.Add(new DateRange { date = Convert.ToDateTime("11-Aug-2018") });
lstDate.Add(new DateRange { date = Convert.ToDateTime("12-Aug-2018") });
lstDate.Add(new DateRange { date = Convert.ToDateTime("13-Aug-2018") });
return lstDate;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.