Last active
November 26, 2015 03:11
-
-
Save macecchi/a17c5d0373ab68067735 to your computer and use it in GitHub Desktop.
NSDate+nextHour category (next round hour)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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