如何获得时间间隔,如在NSDate例如2m,3h,星期二,2014-06-01的消息中所示

时间:2014-06-19 10:16:15

标签: ios time nsdate intervals messages

是否有框架或API将给定NSDate的时间间隔(如2m,3h,星期二,2014-06-01)提供给当前的NSDate。它广泛用于iMessages和Mail App。它也用在其他应用程序中,我只是看到在我尝试为自己编写一个框架之前是否有框架。

1 个答案:

答案 0 :(得分:2)

我建议使用this类别。它对你正在寻找的东西非常有帮助。

例如这行代码:

NSString *displayString = [NSDate stringForDisplayFromDate:date];

产生以下类型的输出:

 - ‘3:42 AM’ – if the date is after midnight today
 - ‘Tuesday’ – if the date is within the last seven days
 - ‘Mar 1’ – if the date is within the current calendar year
 - ‘Mar 1, 2008’ – else ;-)