objective c - Calculate Days Between Dates, Ignoring Certain Weekdays -
i developing scheduling application, , part of events repeat, say, daily in given schedule. schedule 'active' on days—think of work week, saturdays , sundays nothing happens.
i'd calculate number of consecutive days between 2 dates, excluding weekdays. have method uses nscalendar
this, ignoring excluded days, did not see methods on (or related classes) that'd allow me ignore days.
+ (nsinteger) okdaysbetweendate:(nonnull nsdate *) fromdatetime anddate:(nonnull nsdate *) todatetime { nsdate *fromdate; nsdate *todate; nscalendar *calendar = [nscalendar currentcalendar]; [calendar rangeofunit:nscalendarunitday startdate:&fromdate interval:null fordate:fromdatetime]; [calendar rangeofunit:nscalendarunitday startdate:&todate interval:null fordate:todatetime]; nsdatecomponents *difference = [calendar components:nscalendarunitday fromdate:fromdate todate:todate options:0]; return [difference day]; }
a possibility have thought of checking how many weeks between 2 dates, multiplying number of excluded days week, , subtracting end result, doesn't seem particularly clean me.
is there better, more cocoa-y (perhaps using nscalendar
or friends) way of implementing this?
thanks.
Comments
Post a Comment