views:

15

answers:

1

Hi,

I have two nsdatecomponent object, and I want a substract the time of my first object with the time of the seconde object.

example: DateComponentObject1 = DateComponentObject1 - DateComponentObject2

so, if I have 3 hour in DateComponentObject1 and 1 hour in DateComponentObject2, I have 2 hour at the end in the DateComponentObject1.

How I can do this?

Alex

+2  A: 
  1. Create an NSCalendar object that corresponds to the calendar used by your NSDateComponent instances.
  2. Convert DateComponentObject1 to an NSDate with -[NSCalendar dateFromComponents:].
  3. Multiply all values in DateComponentObject2 by -1 (because you want to subtract them from the first date).
  4. Add the inverted DateComponentObject2 to the date object with -[NSCalendar dateByAddingComponents:toDate:options:].
  5. Split the resulting NSDate object into date components with -[NSCalendar components:fromDate:].
Ole Begemann