Skip to content

Instantly share code, notes, and snippets.

@macecchi
Last active November 26, 2015 03:11
Show Gist options
  • Save macecchi/a17c5d0373ab68067735 to your computer and use it in GitHub Desktop.
Save macecchi/a17c5d0373ab68067735 to your computer and use it in GitHub Desktop.
NSDate+nextHour category (next round hour)
#import <Foundation/Foundation.h>
@interface NSDate(nextHour)
/**
* Gets the next full hour from current date. For example, if now is 16:16:45, returns 17:00:00.
*
* @return NSDate with next full hour (current hour + 1), 0 minutes and 0 seconds.
*/
+ (NSDate *)nextHour;
@end
#import "NSDate+nextHour.h"
@implementation NSDate(nextHour)
+ (NSDate *)nextHour {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour fromDate:now];
components.hour++;
return [calendar dateFromComponents:components];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment