How could I determine the number of days between two NSDate values (taking into consideration time as well)?
The NSDate values are in whatever form [NSDate date] takes.
Specifically, when a user enters the inactive state in my iPhone app, I store the following value:
exitDate = [NSDate date];
And when they open the app back up, I get the current time:
NSDate *now = [NSDate date];
Now I’d like to implement the following:
-(int)numberOfDaysBetweenStartDate:exitDate andEndDate:now
Here’s an implementation I used to determine the number of calendar days between two dates:
EDIT:
Fantastic solution above, here’s Swift version below as an extension on
NSDate:A bit of force unwrapping going on which you may want to remove depending on your use case.
The above solution also works for time zones other than the current time zone, perfect for an app that shows information about places all around the world.