Здесь решение префекта, чтобы найти разницу между двумя датами
- (NSString *)calculateDuration:(NSDate *)oldTime secondDate:(NSDate *)currentTime
{
NSString *timeSincePost;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:oldTime toDate:currentTime options:0];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
if (year) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)year,[[kAppDelegate languageBundle] localizedStringForKey:@"y" value:@"" table:nil]];
}
else if (month) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)month,[[kAppDelegate languageBundle] localizedStringForKey:@"M" value:@"" table:nil]];
}
if(day) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)day,[[kAppDelegate languageBundle] localizedStringForKey:@"d" value:@"" table:nil]];
}
else if(hour) {
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)hour,[[kAppDelegate languageBundle] localizedStringForKey:@"H" value:@"" table:nil]];
}
else if(minute) {
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)minute,[[kAppDelegate languageBundle] localizedStringForKey:@"m" value:@"" table:nil]];
}
else if(second)
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)second,[[kAppDelegate languageBundle] localizedStringForKey:@"s" value:@"" table:nil]];
return timeSincePost;
}
и вызов вышеуказанной функции с двумя параметрами как NSDate
NSString *duration = [self calculateDuration:postDate secondDate:[NSDate date]];
lblPostTime.text = duration;
note :: postDate - FirstDate, а вторая дата - текущая дата.