I am storing all dates in a NSMutableArray with the format @"yyyy-MM-dd". I have a date and I am adding a week to that event until I reach the end of the year.
So, if I have the date 2016-05-05 it will add a week to that date and store it in the array until it reaches 2016-12-31.
The code I have works fine, the ONLY problem I have is that when it reaches November, it misses one day. Here is an example:
Date starts at 2016-10-17 I add a week and I get 2016-10-24. and then store it in Array. I add another week (2016-10-31) and store it in array. I add another week and I get 2016-11-06 instead of 2016-11-07.
I can't see why this happens. Here is the code:
if([anEvent[3] isEqualToString:@"Every Week"])
{
//date from events
NSDate *from = [dateFormatter dateFromString:anEvent[1]];
//NSDate *to = [dateFormatter dateFromString:anEvent[2]];
NSDate *nextWeek = from;
while ([_calendar yearOfDate:nextWeek] == [_calendar yearOfDate:[NSDate date]]){
nextWeek = [nextWeek dateByAddingTimeInterval:7*24*60*60];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dates = [dateFormatter stringFromDate:nextWeek];
[everyWeekDates addObject:dates];
};
[everyWeekDates removeLastObject];
}
Any ideas why this happens?
NOTE:
I discover that this happens when I add the date to the array in format @"yyyy-MM-dd". When I store the event date as a NSDate in the array it works fine.