如何在一年中的变化中找到下个月和前几个月

时间:2022-03-13 04:38:03

I am having two text boxes(startdate,enddate). If The startdate and enddate values are between 21st of any month and 20th of next month from startDate month means, i can do anything Else not.

我有两个文本框(startdate,enddate)。如果startdate和startdate值在startDate月份的任何月份的21日和下个月的20日之间,则表示我可以做任何其他事情。

for example,

startdate(25/jan/2017) and enddate(20/feb/2017) : can do any logic

startdate(25 / jan / 2017)和enddate(20 / feb / 2017):可以做任何逻辑

startdate(25/jan/2017)and enddate(21/feb/2017): not possible to do the logic

startdate(25 / jan / 2017)和enddate(21 / feb / 2017):不可能做逻辑

i used some logic for that,

我用了一些逻辑,

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))
            {
            }

but it was not working for startdate(in December month) and endDate(in January month) because of this ((startDate2.Day <= 20 && endDate2.Day >= 21 && emonth == smonth - 1)) can anyone help me with correct logic?

但由于这个原因((startDate2.Day <= 20 && endDate2.Day> = 21 && emonth == smonth - 1),它不适用于startdate(12月份)和endDate(1月份))任何人都可以帮助我有正确的逻辑吗?

2 个解决方案

#1


0  

Probably because you are expecting the end month be exactly one less than the starting month

可能是因为您预计结束月份比起始月份少一个月

emonth == smonth -1

Which means it can't work if you compare December to January

这意味着如果比较12月到1月它就无法工作

1 == 12-1 => false

Not providing any full solutions, but want to help you little by little to understand ;)

没有提供任何完整的解决方案,但希望一点一点地帮助你理解;)

#2


0  

Assuming that smonthand emonth are int and define the month from 1 to 12, you could use modulo on the months:

假设smonthand emonth为int并定义从1到12的月份,您可以在月份上使用modulo:

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))

Note, however, that this will still allow e.g. Dec 23 2015 until Jan 12 2017.

但请注意,这仍然允许例如2015年12月23日至2017年1月12日。

To avoid this, you should compare the year as well or implement an additional condition limiting endDate-startDate to 31 days:

为避免这种情况,您还应该比较年份或实施将endDate-startDate限制为31天的附加条件:

endDate <= startDate.AddDays(31);

#1


0  

Probably because you are expecting the end month be exactly one less than the starting month

可能是因为您预计结束月份比起始月份少一个月

emonth == smonth -1

Which means it can't work if you compare December to January

这意味着如果比较12月到1月它就无法工作

1 == 12-1 => false

Not providing any full solutions, but want to help you little by little to understand ;)

没有提供任何完整的解决方案,但希望一点一点地帮助你理解;)

#2


0  

Assuming that smonthand emonth are int and define the month from 1 to 12, you could use modulo on the months:

假设smonthand emonth为int并定义从1到12的月份,您可以在月份上使用modulo:

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))

Note, however, that this will still allow e.g. Dec 23 2015 until Jan 12 2017.

但请注意,这仍然允许例如2015年12月23日至2017年1月12日。

To avoid this, you should compare the year as well or implement an additional condition limiting endDate-startDate to 31 days:

为避免这种情况,您还应该比较年份或实施将endDate-startDate限制为31天的附加条件:

endDate <= startDate.AddDays(31);