如何在Objective-C中格式化Facebook / Twitter日期(来自JSON提要) [英] How to format Facebook/Twitter dates (from the JSON feed) in Objective-C
问题描述
对于我的iPhone应用程序...
For my iPhone application...
我正在寻找一种格式化Facebook的方法 created_time
JSON物业价值。
I am looking for a way to format the Facebook created_time
JSON property value.
created_time:2010-12-01T21:35:43 + 0000
我也在寻找一种格式化Twitter日期的方法。
I am also looking for a way to format the Twitter date.
created_at :Wed Dec 01 17:08:03 +0000 2010
我希望将两者都更改为<$ c格式$ c>周四2010年11月18日上午06:48 ,或类似的东西。我没有运气使用 NSDateFormatter
。
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
.
如何使用Objective-C从Facebook和Twitter格式化这些日期进入我想要的格式?
How can i format these dates from Facebook and Twitter using Objective-C into the format i desire?
推荐答案
这是我为Twitter工作的:
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];
其中推文
是 NSMutableArray
填充 NSDictionary
对象, storyIndex
作为行int值(在tableview)和 TWITTER_CREATED_AT_JSON_KEY
是一个常量 NSString
,其值为 created_at
。在你希望显示日期的地方使用dateStr
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
和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];
这篇关于如何在Objective-C中格式化Facebook / Twitter日期(来自JSON提要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!