Skip to content

Instantly share code, notes, and snippets.

@bluebanboom
Created June 14, 2012 02:12
Show Gist options
  • Save bluebanboom/2927707 to your computer and use it in GitHub Desktop.
Save bluebanboom/2927707 to your computer and use it in GitHub Desktop.
Example of formatting the date string
//
// Example of formatting the date string
// "Tue, 16 Dec 2008 11:45:13 +0000"
// to
// "16. December 2008"
//
NSString *feedDateString = @"Tue, 16 Dec 2008 11:45:13 +0000";
//
// A formatter to create a date using the RFC2822 date of a RSS feed
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
// Note: We have to force the locale to "en_US" to avoid unexpected issues formatting data
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[inputFormatter setLocale: usLocale];
[usLocale release];
// set the format based on an RFC2822 date format
[inputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
// create a NSDate object using the NSDateFormatter
NSDate *formattedDate = [inputFormatter dateFromString: feedDateString];
// format the date created before to a format of your choice, such as "16. December 2008"
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"d’.’ MMMM yyyy"];
NSLog(@"formattedDateString : ‘%@’", [outputFormatter stringFromDate:formattedDate]);
[inputFormatter release];
[outputFormatter release];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment