我对字段"From“和"To”有一个日期选择器,我想要减法的结果。例如: toValue-fromValue,结果将以小时为单位。如何做到这一点?
发布于 2011-07-09 04:22:45
可以使用timeIntervalSinceDate计算两个NSdate对象之间的差异
NSTimeInterval diff = [toDate timeIntervalSinceDate:fromDate];结果以秒为单位给出。然后,按如下方式计算小时数:
NSInteger hours = diff / 3600;发布于 2011-07-09 04:23:39
如果有两个NSDate对象,则可以使用timeIntervalSinceDate方法对它们进行比较
NSDate* fromDate = //Get from date picker
NSDate* toDate = //Get from date picker
NSTimeInterval = [fromDate timeIntervalSinceDate:toDate];
NSInteger hours = timeInterval / 60*60; //60 seconds per minute, 60 minutes per hourhttps://stackoverflow.com/questions/6630040
复制相似问题