NSDateFormatter subtraction
I have a datepicker for the field "From" and "To" and I want the result of the s开发者_如何转开发ubtraction. for example: toValue-fromValue and the result would be in hours. How to do that?
The difference of the two NSdate
objects can be calculated using timeIntervalSinceDate
:
NSTimeInterval diff = [toDate timeIntervalSinceDate:fromDate];
The result is given in seconds. Then, you calculate the hours like this:
NSInteger hours = diff / 3600;
If you have two NSDate objects you can compare them using the timeIntervalSinceDate
method
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
精彩评论