Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can set a date string from twitter to a NSDATE

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;
like image 401
Jason Avatar asked Aug 07 '09 15:08

Jason


1 Answers

i had the same question, and i could not resolve it with the current above answers. so here is what worked for me:

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    //Wed Dec 01 17:08:03 +0000 2010
    [df setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
    NSDate *date = [df dateFromString:[[tweets objectAtIndex: storyIndex] objectForKey: TWITTER_CREATED_AT_JSON_KEY]];
    [df setDateFormat:@"eee MMM dd yyyy"];
    NSString *dateStr = [df stringFromDate:date];

where tweets is an NSMutableArray filled with NSDictionary objects, storyIndex being the row int value (in the tableview), and TWITTER_CREATED_AT_JSON_KEY being a constant NSString with value created_at. use the dateStr wherever you wish to display the date

like image 122
james Avatar answered Sep 23 '22 02:09

james