views:

414

answers:

1

Hi, I am pulling data from an RSS Feed. One of the keys in the feed is is a string representing the date and time the item was created.

I am trying to convert this string value to an NSDate. The string value is returned from the RSS feed as: 2009-11-18T22:08:00+00:00

I tried the following code to no avail:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyyMMdd HH:mm"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: @"UsersDate"]];

Ideally; on top of converting the value to a NSDate value, I would also like to format it using the localised date format on the handset.

Any pointers would be a great help.

Kind Regards

+4  A: 

To retrieve the dateFormat:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: @"UsersDate"]];

You can use the predefined formats if you would like to format time and date according to the user's locale:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(@"%@",[dateFormatter stringFromDate:someDate]);
[dateFormatter release]; // don't forget to release the dateformatter

Check the documentation to see which formatter suits you best.

Felix
sorry forgot to answer the question about parsing the date from string. see my updated response
Felix
Thank you very much Felix.
Mick Walker