Get difference between two NSDate objects
Nov0
One of the most often asked questions is how to get difference between two NSDate objects. Since one of the most useful advices I’ve got while learning cocoa is using what’s there, I’d suggest all the folks look carefully at NSDate class reference documentation and see what it provides. Then just fireup the XCode and write this code into some method:
NSTimeInterval interval = [endDate timeIntervalSinceDate:startDate];
I presume you DO know how to get endDate and startDate object. Otherwise you shouldn’t be reading this. You’ll get the difference in seconds with fractions which is pure double. So you can do with it whatever you want. Feed it to some fancy number formatter, start countdown or what not. None of my business ,)
Oh, and btw, here’s a demo project for you to see this code in action: DateDifference XCode project.
Enjoy this article?
Consider subscribing to our RSS feed!
No Comments
No comments yet.