1

I know this question has been asked before (most notably here: Converting NSString to NSDate (and back again)), but I am having difficulties. I am using an RSS feed parser which returns a date in this format:

Fri, 23 Nov 2012 15:39:00 -0800

I wish to convert it to this format:

Nov. 23 2012

WIth a separate time formatted like this:

3:39:00 PM

Here is the code I currently have:

NSString *RSSDate1=@"Fri, 23 Nov 2012 15:39:00 -0800";
NSDateFormatter *RSSDateFormatter = [[NSDateFormatter alloc] init];
[RSSDateFormatter setDateFormat:@"D, d M Y H:i:s O"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [RSSDateFormatter dateFromString:RSSDate1];
[RSSDateFormatter release];
NSDateFormatter *RSSDateFormatter2=[[NSDateFormatter alloc] init];
[RSSDateFormatter2 setDateStyle:NSDateFormatterShortStyle];
UIAlertView *dateAlert=[[UIAlertView alloc] init];
[dateAlert setTitle:[RSSDateFormatter2 stringFromDate:dateFromString]];
[dateAlert addButtonWithTitle:@"Dismiss"];
[dateAlert show];
[dateAlert release];

Right now, the string is returning nil, so I'm betting my formatting is off. Any help would be appreciated. Thanks!

Community
  • 1
  • 1
Aehmlo
  • 930
  • 1
  • 8
  • 20

2 Answers2

1

You might try using this configured NSDateFormatter to convert the current date (now) to an NSString and print out the string. Look at the format that's output and compare it to the format you want. The difference(s) should clue you in as to what you need to change to get your desired format.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
  • 1
    Good point. Doing `NSLog(@"%@", [RSSDateFormatter stringFromDate:[NSDate date]]);` will show you the format the date formatter is expecting. – Hot Licks Nov 24 '12 at 02:41
0

Change it to,

NSString *RSSDate1 = @"Fri, 23 Nov 2012 15:39:00 -0800";
NSDateFormatter *RSSDateFormatter = [[NSDateFormatter alloc] init];
[RSSDateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzzzz"];

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [RSSDateFormatter dateFromString:RSSDate1];
NSLog(@"dateFromString = %@", dateFromString);    
[RSSDateFormatter release];

NSDateFormatter *RSSDateFormatter2 = [[NSDateFormatter alloc] init];
[RSSDateFormatter2 setDateFormat:@"MMM dd, yyyy hh:mm aaa"];
NSLog(@"dateToString = %@", [RSSDateFormatter2 stringFromDate:dateFromString]);

UIAlertView *dateAlert = [[UIAlertView alloc] init];
[dateAlert setTitle:[RSSDateFormatter2 stringFromDate:dateFromString]];
[dateAlert addButtonWithTitle:@"Dismiss"];
[dateAlert show];
[dateAlert release];

Output:

dateToString = Nov 23, 2012 03:39 PM

Note that the format for "Fri, 23 Nov 2012 15:39:00 -0800" is "EEE, dd MMM yyyy HH:mm:ss zzzzz" and for "Nov 23, 2012 03:39 PM" it is "MMM dd, yyyy hh:mm aaa". Each letters in the format represents the corresponding word in date string. For eg:- EEE is for Fri, aaa for PM etc.. Check this for more information on how to do this formatting.

iDev
  • 23,310
  • 7
  • 60
  • 85
  • 1
    Thanks, worked like a charm! I see where my error was now. Makes sense. That's one picky API. – Aehmlo Nov 24 '12 at 03:14