【问题标题】:Convert time format to relative time Objective C将时间格式转换为相对时间 Objective C
【发布时间】:2011-05-14 20:32:46
【问题描述】:

如何在 Objective C 中将 2011-05-08T22:08:38Z 转换为相对时间格式?

【问题讨论】:

标签: objective-c timezone


【解决方案1】:
// 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];

【讨论】:

  • 甜蜜。多谢。我只是在努力获得正确的格式。
  • 感谢您提供可重复使用的好答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2015-03-23
  • 1970-01-01
  • 2018-08-21
相关资源
最近更新 更多