如何计算iOS中两个日期之间的小时数 [英] How to calculate time in hours between two dates in iOS
本文介绍了如何计算iOS中两个日期之间的小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算iOS中两次(可能在不同日期发生)之间的小时数?
How can I calculate the time elapsed in hours between two times (possibly occurring on different days) in iOS?
推荐答案
NSDate 函数 timeIntervalSinceDate:将为您提供两个日期的差异,以秒为单位。
The NSDate function timeIntervalSinceDate: will give you the difference of two dates in seconds.
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
请参阅苹果参考库 http://developer.apple.com/library/mac/navigation/
或者如果您使用的是Xcode只需从菜单中选择帮助/文档。
请参阅:如何转换-nstimeinterval-seconds-into-minutes
- 编辑:请参阅下面的ÐąrέÐέvil的答案,以正确处理夏令时/闰秒
这篇关于如何计算iOS中两个日期之间的小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文