Skip to content

Instantly share code, notes, and snippets.

@fabb
Created July 2, 2013 10:51
Show Gist options
  • Save fabb/5908388 to your computer and use it in GitHub Desktop.
Save fabb/5908388 to your computer and use it in GitHub Desktop.
format current NSDate as ISO8601 string despite user's Calendar or 24-Hour-Format setting
- (NSString *)currentGregorianISO8601Date {
NSDate *requestDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
// WORKAROUND http://openradar.appspot.com/radar?id=1110403
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"US"]];
// translate to Gregorian calendar if other calendar is selected
NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateFormatter setCalendar:gregorian];
NSString *dateString = [dateFormatter stringFromDate:requestDate];
NSLog(@"%@",dateString);
return dateString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment