Jeet Kumar
Jeet Kumar

Reputation: 195

How to calculate number of decades between two dates

There are easy ways to calculate number of days/months/years between two NSDate Objects. It can be done as follows for date( from apple documentation: https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html )

But I want to calculate number of decades between two NSDate objects. Any pointers?

For example 1-Jan-1970 to 5-May-1987 has two decades, that is, 1970-1979 and 1980-1989.

Upvotes: 2

Views: 2727

Answers (1)

siburb
siburb

Reputation: 5017

@Abbath's answer will give you a value of 1 (therefore the number of whole decades between the two dates).

It sounds like you actually want the answer in your example to be 2, for the 70s and 80s even though the whole decade of the 80s is not included.

If you ceil the result instead of flooring it, that should give you the answer you're looking for.

Upvotes: 2

Related Questions