Imagine you had two NSDate objects and you’d like to find out the time interval between those dates. NSDate objects alone won’t help us out there unless we do some serious NSDateFormatting and hair pulling.
Lucky for us there are a few other classes available that will help us do this, namely NSCalendar and NSDateComponents.
Years, Months and Days
Here’s how you can determine how many years, months and days have passed between two NSDates:
NSDate *earlier = [[NSDate alloc]initWithTimeIntervalSinceReferenceDate:1]; NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; // pass as many or as little units as you like here, separated by pipes NSUInteger units = NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit; NSDateComponents *components = [gregorian components:units fromDate:earlier toDate:today options:0]; NSInteger years = [components year]; NSInteger months = [components month]; NSInteger days = [components day]; NSLog(@"Years: %ld, Months: %ld, Days: %ld", (long)years, (long)months, (long)days);
The above example returns Years: 12, Months: 2, Days: 5.
In a nutshell we split the date into “components” such as years, months and days, and let the NSDateComponents class give us those as NSIntegers (i.e. long integers). For the class to calculate this correctly we need to put our days into the context of an NSCalendar (gregorian in our case). This is important because different calendars may return different time intervals.
Just the Days
If you’re interested in just one particular item (days for example) just leave out any other component on this line:
// just the days NSUInteger units = NSDayCalendarUnit;
How about other units?
You can pass as many of those fast enumeration units as you like, separated by pipes. For a full list of available values check out the NSCalendar Class Reference
Further Reading
- NS Calendar Class Reference: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html
- Date and Time Programming Guide: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html