Skip to content

Instantly share code, notes, and snippets.

@jungchris
Created October 23, 2015 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jungchris/93727d8fbc1a81cc608e to your computer and use it in GitHub Desktop.
Save jungchris/93727d8fbc1a81cc608e to your computer and use it in GitHub Desktop.
Converting ISO8601 date-times to NSDate and vice-versa
#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