Skip to content

Instantly share code, notes, and snippets.

@popthestack
Last active December 19, 2015 01:39
Show Gist options
  • Save popthestack/5877965 to your computer and use it in GitHub Desktop.
Save popthestack/5877965 to your computer and use it in GitHub Desktop.
RFC 3339 date string in Objective C
NSDate *now = [[NSDate alloc] init];
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"];
[rfc3339DateFormatter setTimeZone:localTimeZone];
NSString *dateString = [rfc3339DateFormatter stringFromDate:now];
NSLog(@"RFC 3339 datetime: %@", dateString);
// 2013-06-27T10:27:08-0600
// 2013-07-22T15:09:30+0100
@djimondev
Copy link

So many thanks !
This fix 2 problems :

  1. when user is setting to 12h display mode output is correctly displayed as 24h mode.
  2. when an other timezone is setted (like Dublin => +0000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment