开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜