schmidt9 - 10 months ago 72

Objective-C Question

Since NSDate represents "a single point in time, independent of any particular calendrical system or time zone", is it OK to make direct calculations with seconds given by its methods like

`timeIntervalSince1970`

I mean operations like add and subtract of two

`NSTimeInterval`

`double`

`dateByAddingTimeInterval:`

`NSCalendar`

`- (nullable NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSCalendarOptions)opts`

What issues would have such direct calculations?

Answer Source

Adding may make less sense, but subtracting does make sense if you want to find the difference in time between two dates.

What you want to note, however, is that you should only go for the seconds, minutes, hours, days, weeks difference from your subtraction

But to find the difference in months and years, you will want to use the `NSDate`

API. Months and years are not constant

Likewise with adding dates, if you want to add seconds, minutes, hours, days, weeks. Those are constant and easy to add. But Months and Years will trip you up