Created
February 25, 2015 23:09
-
-
Save maddiesch/98eba89bde77638308ed to your computer and use it in GitHub Desktop.
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 (Additions) | |
+ (instancetype)dateWithISO8601DateString:(NSString *)date; | |
+ (instancetype)dateWithISO8601DateTimeString:(NSString *)date; | |
@end | |
@interface NSDateFormatter (Additions) | |
+ (instancetype)ISO8601DateFormatter; | |
+ (instancetype)ISO8601DateTimeFormatter; | |
@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
@implementation NSDate (Additions) | |
+ (instancetype)dateWithISO8601DateString:(NSString *)date { | |
return [[NSDateFormatter ISO8601DateFormatter] dateFromString:date]; | |
} | |
+ (instancetype)dateWithISO8601DateTimeString:(NSString *)date { | |
return [[NSDateFormatter ISO8601DateTimeFormatter] dateFromString:date]; | |
} | |
@end | |
@implementation NSDateFormatter (Additions) | |
+ (instancetype)ISO8601DateFormatter { | |
static NSDateFormatter *fmt; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
fmt = [[NSDateFormatter alloc] init]; | |
fmt.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; | |
fmt.dateFormat = @"yyyy-MM-dd"; | |
}); | |
return fmt; | |
} | |
+ (instancetype)ISO8601DateTimeFormatter { | |
static NSDateFormatter *fmt; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
fmt = [[NSDateFormatter alloc] init]; | |
fmt.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; | |
fmt.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ"; | |
}); | |
return fmt; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment