How to format Facebook/Twitter dates (from the JSON feed) in Objective-C

前端 未结 8 973
有刺的猬
有刺的猬 2020-12-13 02:39

For my iPhone application...

I am looking for a way to format the Facebook created_time JSON property value.

\"created_time\": \"2010-12-0

相关标签:
8条回答
  • 2020-12-13 03:11

    @binnyb's answer is perfect for Objective-C; for those looking to do the same thing using Swift, here's a translated version of his answer:

    For the Twitter Date:

        var df = NSDateFormatter()
        //Wed Dec 01 17:08:03 +0000 2010
        df.dateFormat = "eee MMM dd HH:mm:ss ZZZZ yyyy"
        var date = df.dateFromString(twitterDateString)
        df.dateFormat = "eee MMM dd yyyy"
        var dateStr = df.stringFromDate(date!)
    

    For the Facebook Date:

        var df = NSDateFormatter()
        //Wed Dec 01 17:08:03 +0000 2010
        df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
        var date = df.dateFromString(facebookDateString)
        df.dateFormat = "eee MMM dd yyyy"
        var dateStr = df.stringFromDate(date!)
    

    where twitterDateString and facebookDateString are the date strings obtained from their respective APIs.

    0 讨论(0)
  • 2020-12-13 03:11

    Just to complicate things a bit, the JSON response of the Twitter Search API currently returns a date format (inherited from Summize) which is something like:

    @"Sun, 05 Jun 2011 12:25:47 +0000"
    

    To convert this to NSDate, use:

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
    NSDate *date = [dateFormatter dateFromString:@"Sun, 05 Jun 2011 12:25:47 +0000"];
    
    0 讨论(0)
  • 2020-12-13 03:17

    I give you the solution to parse Facebook api date time:

    parser = [[NSDateFormatter alloc] init];
    [parser setTimeStyle:NSDateFormatterFullStyle];
    [parser setFormatterBehavior:NSDateFormatterBehavior10_4];
    [parser setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"];
    

    Enjoy !

    eBuildy, french Web Agency

    0 讨论(0)
  • 2020-12-13 03:18

    here is what i have working for Twitter:

    NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
        //Wed Dec 01 17:08:03 +0000 2010
        [df setDateFormat:@"eee MMM dd HH:mm:ss ZZZZ yyyy"];
        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

    and Facebook:

    NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
        //2010-12-01T21:35:43+0000  
        [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"];
        NSDate *date = [df dateFromString:[[facebook objectForKey:FACEBOOK_POST_CREATED_TIME_JSON_KEY] stringByReplacingOccurrencesOfString:@"T" withString:@""]];
        [df setDateFormat:@"eee MMM dd, yyyy hh:mm"];
        NSString *dateStr = [df stringFromDate:date];
    
    0 讨论(0)
  • 2020-12-13 03:20
    NSString *yourInputFBdate = @"2010-12-02T20:12:06+0000";
    NSString *fbDate = [yourInputFBdate stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    fbDate = [fbDate stringByReplacingOccurrencesOfString:@"+" withString:@" +"];
    NSDate *date = [[NSDate alloc] initWithString:fbDate];
    
    /// do something with date ...
    
    [date release];
    
    0 讨论(0)
  • 2020-12-13 03:24

    something similar to this should do the first one for you. i would double check against the docs, as i may have capitalised the wrong letters.

    [dateFormatter setDateFormat:@"YYYY-MM-ddThh:mm:ss z"];
    NSDate *date=[NSDateFormatter dateFromString:twitterDate];
    
    0 讨论(0)
提交回复
热议问题