user1237473
user1237473

Reputation: 69

How to apply Time format in iPhone

Hello I am new to iPhone development and I am making an app which recieves the date and time from web services like this 2012-02-23T00:14:13.853. I then need to show how old the information is according to the current date.

What I want is a relative time such as in Facebook or Twitter, e.g. 1 day ago, 2 hours ago etc.

So how should we convert that date into such a format?

Upvotes: 0

Views: 264

Answers (2)

Stev_k
Stev_k

Reputation: 2126

You can use Shubhank's code above, but you will have to convert your string into an NSDate first. In case you don't know how to do this:

NSString *receivedString = yourStringFromWebservice;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *receivedDate = [formatter dateFromString:receivedString];
[formatter release];

You can then use the resulting NSDate in the code above.

Upvotes: 1

Shubhank
Shubhank

Reputation: 21805

you can get the difference in time interval like this

NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:dateFromWebService];
NSLog(@" interval %f",interval); 

this will give you time difference in seconds..you can then perform simple math to convert it into hours..days..and months..etc

Upvotes: 0

Related Questions