How do I convert 2011-05-08T22:08:38Z to a relative time format in Objective C?
Asked
Active
Viewed 846 times
2
-
Do you mean 'local time'? If not, what do you mean by 'relative time'? – Jonathan Leffler May 14 '11 at 20:36
-
You might care to look at [My NSDateFormatter Works Only in the iPhone Simulator](http://stackoverflow.com/questions/838893/my-nsdateformatter-works-only-in-the-iphone-simulator). It isn't a direct duplicate, but it is very close. – Jonathan Leffler May 14 '11 at 20:39
-
possible duplicate of [Relative string from NSDate](http://stackoverflow.com/questions/4937230/relative-string-from-nsdate) – jscs May 14 '11 at 23:48
-
With "relative" I guess you mean an output like *"yesterday"*. See the question [Relative string from NSDate](http://stackoverflow.com/questions/4937230/relative-string-from-nsdate) on how to do that. – DarkDust May 14 '11 at 20:47
1 Answers
4
// create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// set the formatter to parse RFC 3339 dates
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
// the formatter parses your date into an NSDate instance
NSDate *date = [formatter dateFromString:@"2011-05-08T22:08:38Z"];
// now to get relative information you can do things like this, which
// will give you the number of seconds since now
NSTimeInterval secondsFromNow = [date timeIntervalSinceNow];
// or this, which will give you the time interval between this data
// and another date
NSTimeInterval secondsFromOther = [date timeIntervalSinceDate:someOtherDateInstance];

Art Gillespie
- 8,747
- 1
- 37
- 34