重叠开始和结束日期 [英] Getting Overlapping Start and End Date

查看:111
本文介绍了重叠开始和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出DateRange,我需要返回与给定时间段重叠的Start和EndDates列表。

Given a DateRange I need to return a list of Start and EndDates that overlaps the given period.

最好的方法是什么?

        static void Main(string[] args)
          {
             //Period 1stMarch to 20th April
             var startDate = new DateTime(2011, 03, 1);
             var endDate = new DateTime(2011, 4, 20); 

             List<BookedPeriod> bookedPeriods=new List<BookedPeriod>();
             bookedPeriods.Add(new BookedPeriod {StartDate = new DateTime(2011, 02, 5), EndDate = new DateTime(2011, 3, 15)});
             bookedPeriods.Add(new BookedPeriod { StartDate = new DateTime(2011, 03, 20), EndDate = new DateTime(2011, 4, 10) });
             bookedPeriods.Add(new BookedPeriod { StartDate = new DateTime(2011, 04, 01), EndDate = new DateTime(2011, 4, 15) });

             List<OverlappedPeriod> myOverllappedPeriods = GetOverllapedPeriods(startDate, endDate, bookedPeriods);
          }

          public static List<OverlappedPeriod>GetOverllapedPeriods(DateTime startDate,DateTime endDate,List<BookedPeriod>bookedPeriods)
          {
             List<OverlappedPeriod>overlappedPeriods=new List<OverlappedPeriod>();

             // Given a DateRange I need to return a list of Start and EndDates that overlaps
             //??how I do i


             return overlappedPeriods;
          }
       }

       public class BookedPeriod
       {
          public DateTime StartDate { get; set; }
          public DateTime EndDate { get; set; }
       }

        public class OverlappedPeriod
       {
          public DateTime StartDate { get; set; }
          public DateTime EndDate { get; set; }
       }



已编辑



我决定进行编辑,以期澄清问题,从而帮助谁来帮助我。
我对重叠与相交感到困惑。
一种情况可能会有所帮助

EDITED

I have decided to edit in the hope of clarifing and therefore helping who has to help me. I get confused by overlapped vs intersect. A scenario might help

我已经预订了从3月11日至4月20日的体育馆订阅

I have booked a subscription to the gym that goes from 01 March 11 to 20 April 11

我要在2011年2月5日至11月3日之间休假

I go on holiday between 05 Feb 11 to 15 Mar 11

我需要获取订阅有效的开始日期和结束日期,不想参加健身房
我应该回去的结果是StartDate = 1 2011年3月EndDate = 2011年3月15日

I need to get the start and end date when my subscription is valid that I will NOT BE GOING TO THE GYM The result I should get back is StartDate=1 March 2011 EndDate =15 March 2011

myAttempt:
静态无效Main()
{
//假日期间
var startDate = new DateTime(2011,02,5);
var endDate = new DateTime(2011,3,15);

myAttempt: static void Main() { //Holiday Period var startDate = new DateTime(2011, 02, 5); var endDate = new DateTime(2011, 3, 15);

             List<BookedPeriod> bookedPeriods = new List<BookedPeriod>();
             bookedPeriods.Add(new BookedPeriod { StartDate = new DateTime(2011, 02, 5), EndDate = new DateTime(2011, 4, 20) });

             List<OverlappedPeriod> overlappedPeriods=new List<OverlappedPeriod>();

             foreach (var bookedPeriod in bookedPeriods)
             {
                DateTime newStartDate = new DateTime();
                DateTime newEndDate = new DateTime();
                OverlappedPeriod overlappedPeriod=new OverlappedPeriod();
                overlappedPeriod.StartDate = newStartDate;
                overlappedPeriod.EndDate = newEndDate;

                GetDateRange(bookedPeriod.StartDate, bookedPeriod.EndDate, out newStartDate, out newEndDate);
                overlappedPeriods.Add(overlappedPeriod);

             }
             //do something with it

          }
           private static void GetDateRange(DateTime startDate,DateTime endDate,out DateTime newStartDate,out DateTime newEndDate)
           {
              /*
               * I need to get the start and end date when my subscription is valid that I will NOT BE GOING TO THE GYM
                   The result I should get back is StartDate=1 March 2011 EndDate =15 March 2011

               */
           }


推荐答案

您要查找的是完全在提供的日期内还是仅部分提供的日期?

Are you looking for dates that are completely within the provided period, or only partially?

完全在范围:

var overlapped = 
    from period in bookedPeriods
    where period.StartDate >= startDate && period.EndDate <= endDate
    select new OverlappedPeriod { StartDate = period.StartDate, EndDate = period.EndDate };
overlappedPeriods = overlapped.ToList();    

部分重叠:

var overlapped = 
    from period in bookedPeriods
    where (period.StartDate >= startDate && period.EndDate <= endDate)
        || (period.EndDate >= startDate && period.EndDate <= endDate)
        || (period.StartDate <= startDate && period.EndDate >= startDate)
        || (period.StartDate <= startDate && period.EndDate >= endDate)
    select new OverlappedPeriod 
    {
        StartDate = new DateTime(Math.Max(period.StartDate.Ticks, startDate.Ticks)),
        EndDate = new DateTime(Math.Min(period.EndDate.Ticks, endDate.Ticks))
    };

overlappedPeriods = overlapped.ToList();    

这篇关于重叠开始和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆