Calculating the number of days between two dates in Objective-C
Solution 1:
NSString *start = @"2010-09-01";
NSString *end = @"2010-12-01";
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay
fromDate:startDate
toDate:endDate
options:0];
components
now holds the difference.
NSLog(@"%ld", [components day]);
Solution 2:
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:
// NSDate+ADNExtensions.h
#import <Cocoa/Cocoa.h>
@interface NSDate (ADNExtensions)
- (NSInteger)numberOfDaysUntil:(NSDate *)aDate;
@end
The implementation file:
// NSDate+ADNExtensions.m
#import "NSDate+ADNExtensions.h"
@implementation NSDate (ADNExtensions)
- (NSInteger)numberOfDaysUntil:(NSDate *)aDate {
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:self toDate:aDate options:0];
return [components day];
}
@end
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):
NSDate *startDate = [NSDate dateWithString:@"2010-11-01 00:00:00 +0000"];
NSDate *endDate = [NSDate dateWithString:@"2010-11-02 00:00:00 +0000"];
NSInteger difference = [startDate numberOfDaysUntil:endDate];
NSLog(@"Diff = %ld", difference);
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.
Solution 3:
Swift 4 implementation
Method call :
let numberOfDays = daysBetweenDates(startDate: fileCreatedDate,endDate: date)
Method Implementation:
func daysBetweenDates(startDate: Date, endDate: Date) -> Int {
let daysBetween = Calendar.current.dateComponents([.day], from: startDate, to: endDate)
print(daysBetween.day!)
return daysBetween.day!
}
Objective C implementation:
Method Call:
int numberOfDaysSinceFileCreation = [self daysBetweenDates: fileCreatedDate
currentDate: today];
Method Implementation:
- (int) daysBetweenDates: (NSDate *)startDate currentDate: (NSDate *)endDate
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponent = [calendar components:NSCalendarUnitDay fromDate:startDate toDate:endDate options:0];
int totalDays = (int)dateComponent.day;
return totalDays;
}