我有一个字段"From"one_answers"To"的日期选择器,我想要减法的结果。例如:toValue-fromValue,结果将以小时为单位。怎么做呢?
两个NSdate
对象的差值可以用timeIntervalSinceDate
计算:
NSTimeInterval diff = [toDate timeIntervalSinceDate:fromDate];
结果以秒为单位给出。然后,像这样计算小时数:
NSInteger hours = diff / 3600;
如果你有两个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 hour