views:

242

answers:

2

I have a string with this value:

2010-05-13 23:17:29

I'd like to format it and am using the following code:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle = NSDateFormatterMediumStyle;
NSDate *formattedDate = [formatter dateFromString:dateString];
[formatter release];

When the debugger reaches the release line, formattedDate shows "invalid CFStringRef" and

Cannot access memory at address 0x0

Any ideas what I'm doing wrong?

+3  A: 

dateFromString is returning nil because it couldn't parse the string containing the date and time. This is because NSDateFormatterMediumStyle specifies a date format like "May 16, 2010" (it actually varies depending on the locale and user settings). This format doesn't match your string.

To parse your string you should set a dateFormat instead of the dateStyle, for example:

formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
Phil Ross
That works. In the above format, how do I display am/pm and time zone?
4thSpace
A: 

Possibly your string is already released.

I also suspect that your date string isn't in the correct format for the formatter, which will give you a null date.

Paul Lynch