开发者

Convert time format to relative time Objective C

How do I convert 2011-05-08T22:08:38Z to a relative time format in开发者_运维百科 Objective C?


// 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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜