Possible Duplicate:
How can I compare two dates, return a number of days.
I have two dates (as NSString in the form "yyyy-mm-dd"), for example:
NSString *start = "2010-11-01";
NSString *end = "2010-12-01";
I'd like to implement:
- (int)numberOfDaysBetween:(NSString *)startDate and:(NSString *)endDate {
}
Thanks!
components
now holds the difference.This code seems to work nicely in Swift 2:
Swift 4 implementation
Method call :
Method Implementation:
Objective C implementation:
Method Call:
Method Implementation:
There is a whole guide to Date and Time Programming. Here is a relevant section which gives you a hint about what to do.
It's where the example code comes from in the other question.
Try and write something based on that and then come back if you have specific questions.
Edit
Okay. Here is how I would write the code in it's most basic form.
First, I would extend NSDate.
The header file:
The implementation file:
This is very rough code. There is no error checking or validating that the second date is later than the first.
And then I would use it like this (running on a 64-bit, Garbage Collected environment):
This is such a shame, because you would have learned a lot more by posting your code and the incorrect outputs and getting more specific help. But if you just want to be a cut-and-paste programmer; take this code and good luck to you.
ObjC Code:
Result:
Swift 3: