I've browsed through the stackoverflow, but none of the solutions didn't work well. I want to generate NSDate array of days in month, this is the code I am using (year and month hardcoded for now):
NSCalendar *c = [NSCalendar currentCalendar];
NSRange days = [c rangeOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitMonth
forDate:today];
for (int i = 1; i <= days.length; i++) {
NSDateComponents *dc = [NSDateComponents new];
[dc setYear:2015];
[dc setMonth:12];
[dc setDay:i];
NSDate *d = [calendar dateFromComponents:dc];
[arrayDates addObject:d];
}
The problem is that the first objects would always be(previous month last day) :
2015-11-30 23:00:00 +0000
and the last would be (not the last day of current month)
2015-12-30 23:00:00 +0000
I've tried setting different locale, calendar options, but nothing helps.. any idea?
Thank you
This feels like a timezone/locale problem. I wonder if it goes away if you set an explicit locale and also an explicit hour in the middle of the day:
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
Then, when you're creating the dates, something like this:
[dc setHour:12];
The reason I think it's a timezone/locale issue is because I tried your code here and it worked exactly right first time.