ios - check if NSDate occur at particular patterns -
i trying create calendar app show events.
some of these events recurring events (daily,weekly).
let's there 2 events eventa (weekly),eventb(daily) created on jan 10 2012.
so when user open current month calendar, need figure out whether these events shown today based on recurring pattern.
right i'm doing in following way:
get event start date
nsdate * startdate generate offset endnote
nsdate *offsetenddate = [startdate datebyaddingyears:10]; i want show these events utpo 10 years start date
now add event occurrence dates array:
//daily if (recurring==1) { nsdate *nextoccurencedate = [startdate datebyaddingdays:1]; bool eventcrossedoffsetdate = no; while (eventcrossedoffsetdate == no) { [datearr addobject:[nextoccurencedate shortdatestring]]; nextoccurencedate = [nextoccurencedate datebyaddingdays:1]; if (![self isdatevalid:nextoccurencedate andenddate:offsetenddate]) { eventcrossedoffsetdate = yes; } } } this generates array of dates event start date current date.
and use array check if current day/user selected day exists show/hide event.
this method compares 2 dates.
-(bool)isdatevalid:(nsdate*)startdate andenddate:(nsdate*)enddate{ if ([startdate compare:enddate] == nsordereddescending) { //"startdate later enddate return no; } else if ([startdate compare:enddate] == nsorderedascending) { //startdate earlier enddate return yes; } else { //dates same return yes; } return no; } this of course job, more events taking time perform calculations , return array.
is there improve ?
for weekly events check if weekday of somedate matches of event:
let eventweekday = calendar.current.component(.weekday, from: event.startdate) let todayweekday = calendar.current.component(.weekday, from: date()) if todayweekday == eventweekday { // today has weekly event. }
Comments
Post a Comment