Objective C - calculating the number of days betwe

2019-01-02 23:20发布

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!

6条回答
三岁会撩人
2楼-- · 2019-01-02 23:45
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]);
查看更多
Emotional °昔
3楼-- · 2019-01-02 23:52

This code seems to work nicely in Swift 2:

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

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

    return components.day
}
查看更多
狗以群分
4楼-- · 2019-01-02 23:53

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;

}
查看更多
放我归山
5楼-- · 2019-01-02 23:54

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.

查看更多
时光不老,我们不散
6楼-- · 2019-01-02 23:58

ObjC Code:

NSDateComponents *dateComponent = [calender components:NSCalendarUnitDay fromDate:startDate toDate:endDate options:0];

Result:

int totalDays = (int)dateComponent.day;
查看更多
在下西门庆
7楼-- · 2019-01-03 00:05

Swift 3:

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!
查看更多
登录 后发表回答