nsdate - Trying to determine if current date is 3 days or less from the end of the month in iOS -


i trying determine if current date in fact 3 days or less end of month. in other words, if in august, alerted if 28,29,30, or 31st. if in february, notified when 25,26,27, or 28 (or 29). in case of leap year, alerted 26th onwards.

my problem not sure how perform such check works month. here code have far:

-(bool)monthendcheck {     nsdatecomponents *components = [[nscalendar currentcalendar] components:nscalendarunitday |  nscalendarunitmonth | nscalendarunityear fromdate:[nsdate date]];    nsinteger day = [components day];    nsinteger month = [components month];    nsinteger year = [components year];     if (month 3 days or less end of month month) {       return yes;     }  else {       return no;     }  } 

because there months 28, 30, , 31 days, dynamic solution, rather creating whole series of if/else statements each , every condition. there way this?

thanks in advance reply.

first have compute start of current day (i.e. today @ 00.00). otherwise, current day not count full day when computing difference between today , start of next month.

nsdate *now = [nsdate date]; nscalendar *cal = [nscalendar currentcalendar]; nsdate *startoftoday; [cal rangeofunit:nscalendarunitday startdate:&startoftoday interval:null fordate:now]; 

computing start of next month can done rangeofunit:... (using "statement expression" fancy :)

nsdate *startofnextmonth = ({     nsdate *startofthismonth;     nstimeinterval lengthofthismonth;     [cal rangeofunit:nscalendarunitmonth startdate:&startofthismonth interval:&lengthofthismonth fordate:now];     [startofthismonth datebyaddingtimeinterval:lengthofthismonth]; }); 

and difference in days:

nsdatecomponents *comp = [cal components:nscalendarunitday fromdate:startoftoday todate:startofnextmonth options:0]; if (comp.day < 4) {     // ... } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -