[英]How to manipulate a NSDate from a NSString
我试图从字典中获取推特帖子的日期并操纵数据以显示“Seconds Ago”,“1天前”,等等。 我已经下载了一个名为FormatterKit的AFNetworking制造商的API表格。 我的问题是我应该如何从下面的字符串中删除:
NSString *postDate = t[@"created_at"];
到可以传递给API的可操作日期? (我不知道如何将它传递给API。我在自述文件中找到的只是下面的代码。)
TTTTimeIntervalFormatter *timeIntervalFormatter = [[TTTTimeIntervalFormatter alloc] init];
[timeIntervalFormatter stringForTimeInterval:0]; // "just now"
[timeIntervalFormatter stringForTimeInterval:-100]; // "1 minute ago"
[timeIntervalFormatter stringForTimeInterval:-8000]; // "2 hours ago"
// Turn idiomatic deictic expressions on / off
[timeIntervalFormatter stringForTimeInterval:-100000]; // "1 day ago"
[timeIntervalFormatter setUsesIdiomaticDeicticExpressions:NO];
[timeIntervalFormatter stringForTimeInterval:-100000]; // "yesterday"
// Customize the present tense deictic expression for
[timeIntervalFormatter setPresentDeicticExpression:@"seconds ago"];
[timeIntervalFormatter stringForTimeInterval:0]; // "seconds ago"
// Expand the time interval for present tense
[timeIntervalFormatter stringForTimeInterval:-3]; // "3 seconds ago"
[timeIntervalFormatter setPresentTimeIntervalMargin:10];
[timeIntervalFormatter stringForTimeInterval:-3]; // "seconds ago"
字典键值:
"created_at" = "Sun Mar 23 21:18:10 +0000 2014";
您似乎需要计算与TTTTimeIntervalFormatter
一起使用的时间间隔。
而且似乎你开始使用某些(我们未知)格式的日期字符串。
第一步是将您的日期字符串转换为NSDate
。 使用NSDateFormatter
设置为与您的字符串匹配的格式来执行此操作。 如果您还不知道,有很多关于如何执行此步骤的现有主题。
将“发布日期”作为NSDate
,您可以计算出这样的时间间隔:
NSDate *postDate = ... // the date calculated from your date string
NSTimeInterval interval = [postDate timeIntervalSinceNow];
现在过去你的TTTTimeIntervalFormatter
interval
。
您需要为这些方法提供时间增量 (发布时间与现在之间的差异):
NSDate *postTime = ...;
NSTimeInterval delta = [postTime timeIntervalSinceNow];
NSString *deltaDescr = [timeIntervalFormatter stringForTimeInterval:timeInterval];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.