开发者

NSDateFormatter subtraction

开发者 https://www.devze.com 2023-03-18 04:04 出处:网络
I have a datepicker for the field \"From\" and \"To\" and I want the result of the s开发者_如何转开发ubtraction.

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

精彩评论

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