Question

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. I have downloaded an API form the same maker of AFNetworking named FormatterKit. 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? (I'm not sure how to pass it to the API either. All I found in the readme is the code below.)

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";
Was it helpful?

Solution

It appears you need to calculate a time interval for use with 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. Do this using an NSDateFormatter set to the format matching your string. 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 *postDate = ... // the date calculated from your date string
NSTimeInterval interval = [postDate timeIntervalSinceNow];

Now past interval to your TTTTimeIntervalFormatter.

OTHER TIPS

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top