Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone - get number of days between two dates
    primarykey
    data
    text
    <p>I'm writing a GTD app for the iPhone. For the due tasks, I want to display something like "Due tomorrow" or "Due yesterday" or "Due July 18th". Obviously, I need to display "Tomorrow" even if the task is less than 24 hours away (e.g. the user checks at 11pm on Saturday and sees there's a task on Sunday at 8am). So, I wrote a method to get the number of days in between two dates. Here's the code...</p> <pre><code>NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm"]; NSDate *nowDate = [dateFormatter dateFromString:@"2010-01-01-15-00"]; NSDate *dueDate = [dateFormatter dateFromString:@"2010-01-02-14-00"]; NSLog(@"NSDate *nowDate = %@", nowDate); NSLog(@"NSDate *dueDate = %@", dueDate); NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *differenceComponents = [calendar components:(NSDayCalendarUnit) fromDate:nowDate toDate:dueDate options:0]; NSLog(@"Days between dates: %d", [differenceComponents day]); </code></pre> <p>... and here's the output:</p> <pre><code>NSDate *nowDate = 2010-01-01 15:00:00 -0700 NSDate *dueDate = 2010-01-02 14:00:00 -0700 Days between dates: 0 </code></pre> <p>As you can see, the method returns incorrect results. It should have returned 1 as the number of days between the two days. What am I doing wrong here?</p> <p>EDIT: I wrote another method. I haven't done extensive unit tests, but so far it seems to work:</p> <pre><code>+ (NSInteger)daysFromDate:(NSDate *)fromDate inTimeZone:(NSTimeZone *)fromTimeZone untilDate:(NSDate *)toDate inTimeZone:(NSTimeZone *)toTimeZone { NSCalendar *calendar = [NSCalendar currentCalendar]; unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; [calendar setTimeZone:fromTimeZone]; NSDateComponents *fromDateComponents = [calendar components:unitFlags fromDate:fromDate]; [calendar setTimeZone:toTimeZone]; NSDateComponents *toDateComponents = [calendar components:unitFlags fromDate:toDate]; [calendar setTimeZone:[NSTimeZone defaultTimeZone]]; NSDate *adjustedFromDate = [calendar dateFromComponents:fromDateComponents]; NSDate *adjustedToDate = [calendar dateFromComponents:toDateComponents]; NSTimeInterval timeIntervalBetweenDates = [adjustedToDate timeIntervalSinceDate:adjustedFromDate]; NSInteger daysBetweenDates = (NSInteger)(timeIntervalBetweenDates / (60.0 * 60.0 * 24.0)); NSDateComponents *midnightBeforeFromDateComponents = [[NSDateComponents alloc] init]; [midnightBeforeFromDateComponents setYear:[fromDateComponents year]]; [midnightBeforeFromDateComponents setMonth:[fromDateComponents month]]; [midnightBeforeFromDateComponents setDay:[fromDateComponents day]]; NSDate *midnightBeforeFromDate = [calendar dateFromComponents:midnightBeforeFromDateComponents]; [midnightBeforeFromDateComponents release]; NSDate *midnightAfterFromDate = [[NSDate alloc] initWithTimeInterval:(60.0 * 60.0 * 24.0) sinceDate:midnightBeforeFromDate]; NSTimeInterval timeIntervalBetweenToDateAndMidnightBeforeFromDate = [adjustedToDate timeIntervalSinceDate:midnightBeforeFromDate]; NSTimeInterval timeIntervalBetweenToDateAndMidnightAfterFromDate = [adjustedToDate timeIntervalSinceDate:midnightAfterFromDate]; if (timeIntervalBetweenToDateAndMidnightBeforeFromDate &lt; 0.0) { // toDate is before the midnight before fromDate timeIntervalBetweenToDateAndMidnightBeforeFromDate -= daysBetweenDates * 60.0 * 60.0 * 24.0; if (timeIntervalBetweenToDateAndMidnightBeforeFromDate &lt; 0.0) daysBetweenDates -= 1; } else if (timeIntervalBetweenToDateAndMidnightAfterFromDate &gt;= 0.0) { // toDate is after the midnight after fromDate timeIntervalBetweenToDateAndMidnightAfterFromDate -= daysBetweenDates * 60.0 * 60.0 * 24.0; if (timeIntervalBetweenToDateAndMidnightAfterFromDate &gt;= 0.0) daysBetweenDates += 1; } [midnightAfterFromDate release]; return daysBetweenDates; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload