Objective-C Question

NSDateComponents issue - set incorrect day

There are many issues similar to mine, but something goes wrong. Why function setDay sets to 1 day less? Where did I go wrong?

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *today = [NSDate date];
NSLog(@"today date = %@", today);
NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear ) fromDate:today];
[components setDay:1];
NSLog(@"modified date = %@", [calendar dateFromComponents:components]);


today date = 2016-07-04 10:01:30 +0000
modified date = 2016-06-30 21:00:00 +0000

Modified date should be 2016-07-01

Answer Source

Likely it is 2016-07-01, 24:00 GMT. NSLog() prints it out for GMT, not your locale time.

See here: Unexpected value from NSDate

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download