Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

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

For my iPhone application...

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

"created_time": "2010-12-01T21:35:43+0000"

I am also looking for a way to format the Twitter date.

"created_at": "Wed Dec 01 17:08:03 +0000 2010"

I wish to change both to be in the format of Thu Nov 18, 2010 06:48 AM, or something similar. I have had no luck using NSDateFormatter.

How can i format these dates from Facebook and Twitter using Objective-C into the format i desire?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...