I have two dates in 24 hours formate like (dd-MMM-yyyy HH:mm). I want to get a difference between these two dates.
My Code : This is getting only dates, I want a number of days and remaining time in hours also. (like 6days 5 hours)
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(@"There are %d days in between the two dates.", numberOfDays);
NSDateComponentsFormatter
can do that
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.allowedUnits = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
NSLog(@"%@", [formatter stringFromTimeInterval:secondsBetween]);
See NSDateComponentsFormatterUnitsStyle
for other styles.