Check if a date range is within a date range

前端 未结 8 2122
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 06:01

I have the following class:

public class Membership
{
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; } // If null then          


        
8条回答
  •  一个人的身影
    2020-12-09 06:28

    So if I understand this correctly - you want to make sure date range 2 is not within date range 1?

    For example:

    startDate1 = 01/01/2011
    
    endDate1 = 01/02/2011
    

    and

    startDate2 = 19/01/2011
    
    endDate2 = 10/02/2011
    

    This should be a simple case of:

    if ((startDate2 >= startDate1 &&  startDate2 <= endDate1) || 
        (endDate2   >= startDate1 && endDate2   <= endDate1))
    

提交回复
热议问题