How to check if a DateTime range is within another 3 month DateTime range

后端 未结 5 1870
长情又很酷
长情又很酷 2021-01-12 08:37

Hi I have a Start Date and End Date per record in a db.

I need to check to see where the time period falls in a 2 year period broken into two lots of quarters then d

5条回答
  •  情深已故
    2021-01-12 09:07

    This should work for you also.

    class Range
    {
        public DateTime Begin { get; private set; }
        public DateTime End { get; private set; }
        public Range(DateTime begin, DateTime end)
        {
            Begin = begin;
            End = end;
        }
    
        public bool Contains(Range range)
        {
            return range.Begin >= Begin && range.End <= End;
        }
    }
    

    and then to use it

            List ranges = new List();
    
            ranges.Add(new Range(DateTime.Now, DateTime.Now.AddMonths(3)));
            ranges.Add(new Range(DateTime.Now.AddMonths(3), DateTime.Now.AddMonths(6)));
    
            Range test = new Range(DateTime.Now.AddMonths(1), DateTime.Now.AddMonths(2));
    
            var hits = ranges.Where(range => range.Contains(test));
    
            MessageBox.Show(hits.Count().ToString());
    

提交回复
热议问题