2

I have a datepicker for the field "From" and "To" and I want the result of the subtraction. for example: toValue-fromValue and the result would be in hours. How to do that?

BlackM
  • 3,927
  • 8
  • 39
  • 69

2 Answers2

2

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;
albertamg
  • 28,492
  • 6
  • 64
  • 71
1

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
DHamrick
  • 8,338
  • 9
  • 45
  • 62