Rectangle 27 1

ios Objective C calculating the number of days between two dates?


func daysBetweenDate(startDate: NSDate, endDate: NSDate) -> Int
{
    let calendar = NSCalendar.currentCalendar()

    let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])

    return components.day
}

This code seems to work nicely in Swift 2:

Note
Rectangle 27 0

ios Objective C calculating the number of days between two dates?


//  NSDate+ADNExtensions.h

#import <Cocoa/Cocoa.h>


@interface NSDate (ADNExtensions)

- (NSInteger)numberOfDaysUntil:(NSDate *)aDate;

@end
//  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
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);

And then I would use it like this (running on a 64-bit, Garbage Collected environment):

Heh, I came in looking for a code snippet like this, and now I'm on a big guilt trip, haha!

It's where the example code comes from in the other question.

Okay. Here is how I would write the code in it's most basic form.

Robert, this isn't a "give me the code" website. Ask a question about something that you are stuck on and you will receive help to solve that problem. People aren't just going to solve your problem for you. The sooner you understand this, the sooner you can get better answers from people on Stack Overflow. I think Abizer was a bit too kind in giving you the code. ;)

There is a whole guide to Date and Time Programming. Here is a relevant section which gives you a hint about what to do.

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.

This is very rough code. There is no error checking or validating that the second date is later than the first.

Try and write something based on that and then come back if you have specific questions.

btw, I am not sure that NSDate method dateWithString existed at all, but at least it was deprecated long time ago, so the second answer is more relevant, you have to use NSDateFormatter now

can you not just please provide code? honestly, I've tried many things. I've tried using Gregarian and I've tried getting an NSTimeInterval. Please just implement the method?

Note
Rectangle 27 0

ios Objective C calculating the number of days between two dates?


NSLog(@"%ld", [components day]);
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

@RamyAlZuhouri, why would you even need f to be static?

@SpacePyro: forget it, it was an pun in response I had said somewhere else.

How exactly do we access it from components?

Note
Rectangle 27 0

ios Objective C calculating the number of days between two dates?


let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let start = formatter.date(from: "2010-09-01")!
let end = formatter.date(from: "2010-12-01")!
let days = Calendar.current.dateComponents([.day], from: start, to: end).day!
Note