Check if a date range is within a date range

前端 未结 8 2130
爱一瞬间的悲伤
爱一瞬间的悲伤 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:44

    public bool DoesAnOfferAlreadyExistWithinTheTimeframeProvided(int RetailerId, DateTime ValidFrom, DateTime ValidTo)
            {
                bool result = true;
    
                try
                {
                    // Obtain the current list of coupons associated to the retailer.
                    List retailerCoupons = PayPalInStore.Data.RetailerCoupon.Find(x => x.RetailerId == RetailerId).ToList();
    
                    // Loop through each coupon and see if the timeframe provided in the NEW coupon doesnt fall between any EZISTING coupon.
                    if (retailerCoupons != null)
                    {
                        foreach (RetailerCoupon coupon in retailerCoupons)
                        {
                            DateTime retailerCouponValidFrom = coupon.DateValidFrom;
                            DateTime retailerCouponValidTo = coupon.DateExpires;
    
                            if ((ValidFrom <= retailerCouponValidFrom && ValidTo <= retailerCouponValidFrom) || (ValidFrom >= retailerCouponValidTo && ValidTo >= retailerCouponValidTo))
                            {
                                return false;
                            }
                        }
                    }
    
                    return result;
                }
            catch (Exception ex)
            {
                this.errorManager.LogError("DoesAnOfferAlreadyExistWithinTheTimeframeProvided failed", ex);
                return result;
            }
        }
    

提交回复
热议问题