Skip to content

Instantly share code, notes, and snippets.

@maddiesch
Created February 25, 2015 23:09
Show Gist options
  • Save maddiesch/98eba89bde77638308ed to your computer and use it in GitHub Desktop.
Save maddiesch/98eba89bde77638308ed to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface NSDate (Additions)
+ (instancetype)dateWithISO8601DateString:(NSString *)date;
+ (instancetype)dateWithISO8601DateTimeString:(NSString *)date;
@end
@interface NSDateFormatter (Additions)
+ (instancetype)ISO8601DateFormatter;
+ (instancetype)ISO8601DateTimeFormatter;
@end
@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