Calculate number of days between two dates



Number of days between two dates ios

Tags: #Number of Days #Between Two dates   #IOS 

Below is the code snippets that allows to get the number of days between one particular date to another particular date.


+ (NSInteger)daysBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime
{
    NSDate *fromDate;
    NSDate *toDate;
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&fromDate
                 interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&toDate
                 interval:NULL forDate:toDateTime];
    
    NSDateComponents *difference = [calendar components:NSDayCalendarUnit
                                               fromDate:fromDate toDate:toDate options:0];
    
    return [difference day];
}

1 thought on “Calculate number of days between two dates”

  1. Pingback: Get number of days between two dates swift - iOSTutorialJunction

Comments are closed.