120

How can I calculate the days between 1 Jan 2010 and (for example) 3 Feb 2010?

9 Answers 9

246
NSDate *date1 = [NSDate dateWithString:@"2010-01-01 00:00:00 +0000"]; NSDate *date2 = [NSDate dateWithString:@"2010-02-03 00:00:00 +0000"]; NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1]; int numberOfDays = secondsBetween / 86400; NSLog(@"There are %d days in between the two dates.", numberOfDays); 

EDIT:

Remember, NSDate objects represent exact moments of time, they do not have any associated time-zone information. When you convert a string to a date using e.g. an NSDateFormatter, the NSDateFormatter converts the time from the configured timezone. Therefore, the number of seconds between two NSDate objects will always be time-zone-agnostic.

Furthermore, this documentation specifies that Cocoa's implementation of time does not account for leap seconds, so if you require such accuracy, you will need to roll your own implementation.

Sign up to request clarification or add additional context in comments.

19 Comments

@RalucaGurau: 86400 is the number of seconds in a day (i.e. 60 seconds, times 60 minuts, times 24 hours). It has nothing to so with the month.
Not all days have 86400 seconds. For a trivial example that's fine but it's not a good idea in the real world. DST changes, leap seconds, etc can all mess with it. NSCalendar can tell you how many seconds are in a given day.
@muthukumar: Not on iOS, the method is only available on Mac OS X. This question is tagged cocoa and not cocoa-touch so this answer only applies to Mac OS X.
The number of days between any two moments in time will always be 86400. Whether humans are in the stage of fooling themselves to wake up an hour earlier than normal or not makes no difference to the fact that a day is 86400 seconds long (ignoring leap seconds)
Just want to add that if you don't have that +0000 on the end, you'll be getting null dates and a value of 0 always on the diff, no matter what. So, don't fall in that trap.
|
85

You may want to use something like this:

NSDateComponents *components; NSInteger days; components = [[NSCalendar currentCalendar] components: NSDayCalendarUnit fromDate: startDate toDate: endDate options: 0]; days = [components day]; 

I believe this method accounts for situations such as dates that span a change in daylight savings.

5 Comments

is there a simple way to also ignore the time for the given dates?
can be accordingly changed for other calendar units :) used for Year unit.
NSDate objects are already time-zone agnostic. The time-zone information is removed when parsing dates with e.g. NSDateFormatter, so that NSDate objects represent exact moments in time. Therefore, if you already have startDate and endDate, these two objects will be x number of seconds apart. Time-zones will have nothing to do with it.
@Benjamin: Why do you say that?
dreamlax, your answer is wrong. Plain and simple. Calculate the number of days between 12 noon January 1 standard time and 12 noon April 1 daylight time, and your formula doesn't work. It is a lazy formula and developers should use the NSDateComponents object to calculate this.
25
NSTimeInterval diff = [date2 timeIntervalSinceDate:date1]; // in seconds 

where date1 and date2 are NSDate's.

Also, note the definition of NSTimeInterval:

typedef double NSTimeInterval; 

1 Comment

Note that NSTimeInterval is really a double, so you can use it like any other number.
19

Checkout this out. It takes care of daylight saving , leap year as it used iOS calendar to calculate.You can change the string and conditions to includes minutes with hours and days.

+(NSString*)remaningTime:(NSDate*)startDate endDate:(NSDate*)endDate { NSDateComponents *components; NSInteger days; NSInteger hour; NSInteger minutes; NSString *durationString; components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: startDate toDate: endDate options: 0]; days = [components day]; hour = [components hour]; minutes = [components minute]; if(days>0) { if(days>1) durationString=[NSString stringWithFormat:@"%d days",days]; else durationString=[NSString stringWithFormat:@"%d day",days]; return durationString; } if(hour>0) { if(hour>1) durationString=[NSString stringWithFormat:@"%d hours",hour]; else durationString=[NSString stringWithFormat:@"%d hour",hour]; return durationString; } if(minutes>0) { if(minutes>1) durationString = [NSString stringWithFormat:@"%d minutes",minutes]; else durationString = [NSString stringWithFormat:@"%d minute",minutes]; return durationString; } return @""; } 

2 Comments

this method is the perfect way of calculating time difference .
just you can keep elseif with all those if conditions . otherwise you will get only one format of time shown .
3

With Swift 5 and iOS 12, according to your needs, you may use one of the two following ways to find the difference between two dates in days.


#1. Using Calendar's dateComponents(_:from:to:) method

import Foundation let calendar = Calendar.current let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))! let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))! let dateComponents = calendar.dateComponents([Calendar.Component.day], from: startDate, to: endDate) print(dateComponents) // prints: day: 1621 isLeapMonth: false print(String(describing: dateComponents.day)) // prints: Optional(1621) 

#2. Using DateComponentsFormatter's string(from:to:) method

import Foundation let calendar = Calendar.current let startDate = calendar.date(from: DateComponents(year: 2010, month: 11, day: 22))! let endDate = calendar.date(from: DateComponents(year: 2015, month: 5, day: 1))! let formatter = DateComponentsFormatter() formatter.unitsStyle = .full formatter.allowedUnits = [NSCalendar.Unit.day] let elapsedTime = formatter.string(from: startDate, to: endDate) print(String(describing: elapsedTime)) // prints: Optional("1,621 days") 

Comments

1

Swift 4
Try this and see (date range with String):

// Start & End date string let startingAt = "01/01/2018" let endingAt = "08/03/2018" // Sample date formatter let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd/MM/yyyy" // start and end date object from string dates var startDate = dateFormatter.date(from: startingAt) ?? Date() let endDate = dateFormatter.date(from: endingAt) ?? Date() // Actual operational logic var dateRange: [String] = [] while startDate <= endDate { let stringDate = dateFormatter.string(from: startDate) startDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) ?? Date() dateRange.append(stringDate) } print("Resulting Array - \(dateRange)") 

Swift 3

var date1 = Date(string: "2010-01-01 00:00:00 +0000") var date2 = Date(string: "2010-02-03 00:00:00 +0000") var secondsBetween: TimeInterval = date2.timeIntervalSince(date1) var numberOfDays: Int = secondsBetween / 86400 print(numberOfDays) 

3 Comments

Not all days have that amount of seconds.
@Pavan - You are welcomed to share your inputs by updating this answer and make it better.
To make it better would require me rewriting your whole answer, in which case it may as well be deleted because it'll be the same solution as the answer above which doesn't use hardcoded values :)
1

If you want all the units, not just the biggest one, use one of these 2 methods (based on @Ankish's answer):

Example output: 28 D | 23 H | 59 M | 59 S

+ (NSString *) remaningTime:(NSDate *)startDate endDate:(NSDate *)endDate { NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; NSDateComponents *components = [[NSCalendar currentCalendar] components:units fromDate: startDate toDate: endDate options: 0]; return [NSString stringWithFormat:@"%ti D | %ti H | %ti M | %ti S", [components day], [components hour], [components minute], [components second]]; } + (NSString *) timeFromNowUntil:(NSDate *)endDate { return [self remaningTime:[NSDate date] endDate:endDate]; } 

Comments

0

You can find the difference by converting the date in seconds and take time interval since 1970 for this and then you can find the difference between two dates.

1 Comment

when two dates can be calculated with each other directly(assuming same time domain). Is there any specific reason of calculating both dates since 1970 first and then finding the difference ?
0

To find the difference, you need to get the current date and the date in the future. In the following case, I used 2 days for an example of the future date. Calculated by:

2 days * 24 hours * 60 minutes * 60 seconds. We expect the number of seconds in 2 days to be 172,800.

// Set the current and future date let now = Date() let nowPlus2Days = Date(timeInterval: 2*24*60*60, since: now) // Get the number of seconds between these two dates let secondsInterval = DateInterval(start: now, end: nowPlus2Days).duration print(secondsInterval) // 172800.0 

1 Comment

Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.