Goal C - calculate the number of days between two dates

Possible duplicate:
How to compare two dates, return the number of days.

I have two dates (like an NSString in the form of "yyyy-mm-dd"), for example:

NSString *start = "2010-11-01"; NSString *end = "2010-12-01"; 

I would like to implement:

 - (int)numberOfDaysBetween:(NSString *)startDate and:(NSString *)endDate { } 

Thank!

+48
ios objective-c nsdate nsstring
Jan 01 2018-10-01T00:
source share
6 answers
 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 matters.

 NSLog(@"%ld", [components day]); 
+159
Jan 02 '10 at 0:50
source share

There is a whole guide to programming date and time. Here is a section that gives you a hint on what to do.

Here, where the sample code arises from another question.

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

Edit

Good. This is how I write the code in it in the most basic form.

First, I would expand NSDate.

Header file:

 // NSDate+ADNExtensions.h #import <Cocoa/Cocoa.h> @interface NSDate (ADNExtensions) - (NSInteger)numberOfDaysUntil:(NSDate *)aDate; @end 

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 crude code. Error checking or verifying that the second date is later than the first.

And then I will use it like this (works in a 64-bit environment with garbage):

 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 incorrect exits and getting more specific help. But if you just want to be a programmer for cutting and pasting; take this code and good luck to you.

+21
Jan 01 2018-10-01T00:
source share

This code works well 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 } 
+6
Aug 23 '15 at 9:39 on
source share

ObjC Code:

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

Result:

 int totalDays = (int)dateComponent.day; 
+3
Sep 23 '15 at 7:33
source share

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! 
+3
Oct 07 '16 at 21:43
source share

Introducing Swift 4

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; } 
+1
Jun 29 '17 at 9:33
source share



All Articles