Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare just month and day from a datetime object

In my C# program I have run into an obstacle where I have a table that stores date ranges (columns are date range ID (int), begin date (DateTime) and end date (DateTime). I want to query the table and get back rows that only fall within a specific date range. I cannot use datetime.date since that includes the year.

So for example, I want to query the table and get all date ranges that fall between 01-01 and 5-31.

I have tried using the following lambda to query the table, but my result set is empty.

List<DateRanges> tempDateRangeList = dataContext
                                     .DateRanges
                                     .Where(r=>r.BeginDate.Month <= startDate.Month 
                                               && r.EndDate.Month >= finishDate.Month)
                                     .ToList();

tempDateRangeList = tempDateRangeList.Where(r=>r.BeginDate.Day <= startDate.Day 
                                               && r.EndDate.Day >= finishDate.Day)
                                     .ToList();

Does anyone have any suggestions on how I could accomplish this?

Edit:

Examples of BeginDate and EndDate would be a list such as follows:

BeginDate 1/1/2016, 5/25/2016, 9/11/2016

EndDates 5/24/2016, 9/10/2016, 12/31/2016

Filter date would be: startDate = 12/8 finishDate = 12/12

Expected result: Begin Date of 9/11 End date of 12/31

like image 807
Stephen Avatar asked Dec 07 '16 21:12

Stephen


1 Answers

There are two cases in your condition - month equal to boundary month, in which case you must test day number, and a different month, in which you ignore day. Hence the query:

List<DateRanges> tempDateRangeList = 
    dataContext.DateRanges.Where(r =>
        ((r.BeginDate.Month < startDate.Month) || 
         (r.BeginDate.Month == startDate.Month && r.BeginDate.Day <= startDate.Day)) &&
        ((r.EndDate.Month > finishDate.Month) ||
         (r.EndDate.Month == finishDate.Month) && r.EndDate.Day >= finsihDate.Day))
    .ToList();

Condition is ugly and very hard to follow but covers all cases. This query returns all records which define date ranges that completely fall under the boundary dates.

If you wish to find records that are just overlapping (completely or partially) with the filtering range, then the query would be:

List<DateRanges> tempDateRangeList = 
    dataContext.DateRanges.Where(r =>
        ((r.BeginDate.Month < endDate.Month) || 
         (r.BeginDate.Month == endDate.Month && r.BeginDate.Day <= endDate.Day)) &&
        ((r.EndDate.Month > startDate.Month) ||
         (r.EndDate.Month == startDate.Month) && r.EndDate.Day >= startDate.Day))
    .ToList();

This condition may bend your mind, but it works fine.

like image 194
Zoran Horvat Avatar answered Sep 30 '22 01:09

Zoran Horvat