简体   繁体   中英

Timer interval with 2 NSDateComponent

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?

  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:] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM