views:

740

answers:

2

The date you get back from twitter is in this format Fri Aug 07 12:40:04 +0000 2009. I am able to assign the value to a NSDate without issue. However, when I attempt to use NSDateFormatter, I get a nil returned to me. What am I missing?

 NSDate *createdAt = [messageData objectForKey:@"created_at"];
 NSDateFormatter *format = [[NSDateFormatter alloc] init];
 [format setDateFormat:@"M/d/yy HH:mm"];

 NSString *dateString = [format stringFromDate:createdAt];


 label.text = dateString;
+2  A: 

If the object associated with the @"created_at" key is a valid NSDate object, this code should work.

However, I'm guessing that it is actually an NSString. If so, it will produce the behavior you're describing.

If I'm right, the code snippet above is assigning an NSString object to an NSDate reference. NSDictionary returns untyped 'id' objects, so the compiler won't give you a type mismatch warning.

You'll have to use NSDateFormatter to parse the string into an NSDate (see dateFromString:).

amrox
A: 

First off, what are you using stringFromDate: for? That's if you already have an NSDate and want to make a string representing it.

Moreover, when you do use the date formatter, you're giving it a format string that doesn't match the date string you're trying to interpret.

Change the format string to match your date strings, and use dateFromString: instead of stringFromDate: (with the attendant changes to your variable declarations), and it should work.

Peter Hosey