如何以分钟/秒为单位计算两个日期的差异

时间:2010-11-18 20:22:12

标签: iphone nsdateformatter

我有两个类型nsdate的日期,我想计算两个日期的差异b / w最后是一个分钟/秒......

我该怎么做..

3 个答案:

答案 0 :(得分:1)

NSDate-timeIntervalSinceDate:方法。


修改

因此:

NSTimeInterval tval = [date1 timeIntervalSinceDate: date2];
int sec = tval % 60,
    min = tval - 60*sec;

答案 1 :(得分:0)

http://iphonedevelopment.blogspot.com/2009/07/category-on-nsdate.html

当你得到你可以转换成时间的日子。

但如果你想要精确的差异,比如它是1.5天

你必须将日期转换为时间戳然后找到差异

iPhone: Convert date string to a relative time stamp

答案 2 :(得分:0)

所以Alexsander想说你接受这个:

double difference = [date1 timeIntervalSinceDate:date2] / 60;
The difference is now in minutes.