简体   繁体   English

如何从NSString中操作NSDate

[英]How to manipulate a NSDate from a NSString

I am attempting to take the date of a twitter post from a dictionary and manipulate the data to show "Seconds Ago," "1 Day Ago," and so on. 我试图从字典中获取推特帖子的日期并操纵数据以显示“Seconds Ago”,“1天前”,等等。 I have downloaded an API form the same maker of AFNetworking named FormatterKit . 我已经下载了一个名为FormatterKit的AFNetworking制造商的API表格。 My question is how am I supposed to go from my string below: 我的问题是我应该如何从下面的字符串中删除:

NSString *postDate = t[@"created_at"];

to a manipulable date that I can pass in to the API? 到可以传递给API的可操作日期? (I'm not sure how to pass it to the API either. All I found in the readme is the code below.) (我不知道如何将它传递给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"

Dictionary Key-Values: 字典键值:

"created_at" = "Sun Mar 23 21:18:10 +0000 2014";

It appears you need to calculate a time interval for use with TTTTimeIntervalFormatter . 您似乎需要计算与TTTTimeIntervalFormatter一起使用的时间间隔。

And it seems you are starting with a date string in some (unknown to us) format. 而且似乎你开始使用某些(我们未知)格式的日期字符串。

Step one is to convert your date string to an NSDate . 第一步是将您的日期字符串转换为NSDate Do this using an NSDateFormatter set to the format matching your string. 使用NSDateFormatter设置为与您的字符串匹配的格式来执行此操作。 There are MANY existing topics on how to do this step if you don't already know. 如果您还不知道,有很多关于如何执行此步骤的现有主题。

Once you have your "post date" as an NSDate , you can calculate the interval doing something like this: 将“发布日期”作为NSDate ,您可以计算出这样的时间间隔:

NSDate *postDate = ... // the date calculated from your date string
NSTimeInterval interval = [postDate timeIntervalSinceNow];

Now past interval to your TTTTimeIntervalFormatter . 现在过去你的TTTTimeIntervalFormatter interval

You need to provide those methods with a time delta (the difference between the time the post was made and now ): 您需要为这些方法提供时间增量 (发布时间与现在之间的差异):

NSDate *postTime = ...;
NSTimeInterval delta = [postTime timeIntervalSinceNow];
NSString *deltaDescr = [timeIntervalFormatter stringForTimeInterval:timeInterval];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM