Created
October 23, 2015 15:40
-
-
Save jungchris/93727d8fbc1a81cc608e to your computer and use it in GitHub Desktop.
Converting ISO8601 date-times to NSDate and vice-versa
This file contains 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
#pragma mark - ISO8601 to NSDate & vice-versa | |
// Convert ISO 8601 standard Zulu date+time to NSDate | |
+ (NSDate*)convertISO8601ToNSDate:(NSString*)isoString { | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; | |
// Always use this locale when parsing fixed format date strings | |
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; | |
[formatter setLocale:posix]; | |
NSDate *date = [formatter dateFromString:isoString]; | |
return date; | |
} | |
+ (NSString*)convertNSDateToISO8601String:(NSDate*)nsDate { | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; | |
[dateFormatter setLocale:enUSPOSIXLocale]; | |
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"]; | |
NSDate *now = [NSDate date]; | |
NSString *iso8601String = [dateFormatter stringFromDate:now]; | |
return iso8601String; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment