There are a few things to take on board here:
- When asking for the date you don't need to ask for minutes and seconds as you scrap them anyway.
- You should probably set a timeZone
- When the hour is 23 you will add 1, which makes 24 which is not right
You don't need to create a new
NSDateComponents
object, you can just configure the one you haveNSCalendar *calendar = [NSCalendar currentCalendar]; calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:[NSDate date]]; components.hour = (components.hour + 1) % 24; components.minute = 0; components.second = 0; NSDate *fireDate = [calendar dateFromComponents:components]; NSLog(@"%@", fireDate);
The use of %
for setting the hour prevents us going over 23