2017-02-10 88 views
2

我有兩個文本框(startdate,enddate)。 如果startdate和enddate值在startDate月份的任何月份的21日和下個月的20日之間表示,我可以做任何事情否則不行。如何查找下一個和前幾個月的變化

例如,

STARTDATE(25/JAN/2017)和結束日期(20 /二月/ 2017):可以做任何邏輯

STARTDATE(25/JAN/2017)和結束日期(21 /月/ 2017年):不可能做邏輯

我用一些邏輯,

if ((startDate2.Day >= 21 && endDate2.Day >= 21 && emonth == smonth) 
    || (startDate2.Day >= 21 && endDate2.Day <= 20 && emonth == smonth + 1)   
    || (startDate2.Day <= 20 && endDate2.Day <= 20 && emonth == smonth) 
    || (startDate2.Day <= 20 && endDate2.Day >= 21 && emonth == smonth - 1)) 
      { 
      } 

,但(在一月份一個月),它不是爲STARTDATE工作(在12月當月)和結束日期,因爲這((startDate2.Day <= 20 && endDate2.Day >= 21 && emonth == smonth - 1))任何人都可以用正確的邏輯幫助我嗎?

+0

應該如從同一個月的8號到15號被允許? – wkl

+0

是的,它允許 – user7415073

回答

0

可能是因爲您所期待的end month整整一個比起始月

emonth == smonth -1 

這意味着如果你比較12至1月不能少工作

1 == 12-1 => false 

不提供任何滿解決方案,但希望一點一點地幫助您理解;)

+0

是的,你是對的。上面的代碼工作得很好,如果它是從(1月21日到20日),而不是從(21日到12月20日) – user7415073

0

假設smonthemonthint1月份定義到12,你可以在幾個月使用模:

if ((startDate2.Day >= 21 && endDate2.Day >= 21 && emonth == smonth) 
    || (startDate2.Day >= 21 && endDate2.Day <= 20 && emonth == ((smonth + 2)%12)-1)   
    || (startDate2.Day <= 20 && endDate2.Day <= 20 && emonth == smonth) 
    || (startDate2.Day <= 20 && endDate2.Day >= 21 && ((emonth+2)%12)-1 == smonth)) 

但是請注意,這將仍允許用戶如2015年12月23日至1月12日2017年

爲了避免這種情況,你應該比較當年一樣好,甚至實現了附加條件限制endDate-startDate 31天:

endDate <= startDate.AddDays(31); 
+0

感謝您的答案,它正在工作。但2016年不適用(2016年12月,2017年1月1日) – user7415073

+0

此答案與本年度無關。你能告訴我哪個開始和結束日期不符合你的要求嗎? – wkl