1

Following is my code:

NSDateComponents *componenetsForEnd = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:endDate];
NSInteger endDay = [componenetsForEnd day];
NSInteger endMonth = [componenetsFoeEnd month];
NSInteger endDayValue = componenetsForEnd.weekday;

Now: when endDate = 2015-08-31 23:59:59 +0000, I am getting month as 9! This is causing wrong endDayValue. What is going wrong here?

EDIT:

Here is how I calculate endDate :

NSDate *endDate = [self endOfMonthForDate:todayDate];

this method takes today's date with [NSDate date] and this is how it goes:

- (NSDate *) dateByAddingMonths: (NSInteger) monthsToAdd andDate: (NSDate *)date
{
NSCalendar * calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];
NSDateComponents * months = [[NSDateComponents alloc] init];
[months setMonth: monthsToAdd];

return [calendar dateByAddingComponents: months toDate: date options: 0];
}

- (NSDate *) endOfMonthForDate: (NSDate *)date
{
NSCalendar * calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];

NSDate * plusOneMonthDate = [self dateByAddingMonths: 1 andDate:date];


NSDateComponents * plusOneMonthDateComponents = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit fromDate: plusOneMonthDate];

NSDate * endOfMonth1 = [[calendar dateFromComponents: plusOneMonthDateComponents] dateByAddingTimeInterval: -1]; // One second before the start of next month
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:endOfMonth1];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:endOfMonth1];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* endOfMonth = [[NSDate alloc] initWithTimeInterval:interval sinceDate:endOfMonth1];

return endOfMonth;
}

Also tried performing these actions to make NSDateComponents and NSDate with dateFormatter in systemTimeZone:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *endDateString = [dateFormatter stringFromDate:endDate];

NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
[dateFormatter2 setTimeZone:[NSTimeZone systemTimeZone]];
dateFormatter2.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *endDateProper = [dateFormatter2 dateFromString:endDateString];

NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *componenetsForEnd = [greCalendar components:NSWeekdayCalendarUnit | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:endDateProper];
[componenetsForEnd setTimeZone:[NSTimeZone systemTimeZone]];
tech savvy
  • 1,417
  • 4
  • 21
  • 42

2 Answers2

0

Instead of using current calendar

[NSCalendar currentCalendar]

you can use:

NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

tuledev
  • 10,177
  • 4
  • 29
  • 49
  • still "endDate = 2015-08-31 23:59:59 +0000, I am getting month as 9?" – tuledev Aug 14 '15 at 12:26
  • yes, month comes as 9 and the whole weekday schedule changes. – tech savvy Aug 14 '15 at 12:27
  • yes, endDate is what I have mentioned in question. For that, when I calculate WeekDay it mentions acc to month = 9 and not 8. – tech savvy Aug 14 '15 at 12:30
  • I had some problems like this before. But I can't remember exactly. I will try to take a flashback. Can you show more about endDate. How to get it? – tuledev Aug 14 '15 at 12:32
  • Let try to change current Calendar ([NSCalendar currentCalendar]) to NSGregorianCalenda, as the code I wrote below. Not sure, but let change and what happen? – tuledev Aug 14 '15 at 12:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86995/discussion-between-anhtu-and-tech-savvy). – tuledev Aug 14 '15 at 12:44
0

The problem was with the fact that I think is this way: When calculating a day, month or Hour, minute and second , you need to specify that timezone for dateComponents is UTC i.e. GMT. However, my endDateProper which is in system timezone.

So, doing this did the trick for me:

NSDateComponents *componenetsForEnd = [greCalendar componentsInTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"] fromDate:endDateProper];
tech savvy
  • 1,417
  • 4
  • 21
  • 42