Try this method i hope its helpfull
- (NSString*)getTimeDifferenceFromCurrentDate:(NSDate *) lastPostDate {
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit fromDate:messageDate toDate:currentDate options:0];
NSString *strTime = @"";
/*if (components.day > 0 || components.week >= 1) {
if (components.day >= 1||components.week >= 1||components.month >= 1){
NSDateFormatter *dfForCell = [[NSDateFormatter alloc] init];
[dfForCell setDateFormat:@"MMM dd, yyyy"];
NSLocale* my24HourLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
[dfForCell setLocale:my24HourLocale];
NSString *aStrFinalDate = [dfForCell stringFromDate:messageDate];
strTime = [NSString stringWithFormat:@"%@", aStrFinalDate];
};
}*/
if (components.year > 0) {
if (components.year > 1)
strTime = [NSString stringWithFormat:@"%d years ago", components.year];
else
strTime = [NSString stringWithFormat:@"%d year ago", components.year];
}
else if (components.month > 0) {
if (components.month > 1)
strTime = [NSString stringWithFormat:@"%d months ago", components.month];
else
strTime = [NSString stringWithFormat:@"%d month ago", components.month];
}
else if (components.week > 0) {
if (components.week > 1)
strTime = [NSString stringWithFormat:@"%d weeks ago", components.day];
else
strTime = [NSString stringWithFormat:@"%d day ago", components.day];
}
else if (components.day > 0) {
if (components.day > 1)
strTime = [NSString stringWithFormat:@"%d days ago", components.day];
else
strTime = [NSString stringWithFormat:@"%d day ago", components.day];
}
else if (components.hour > 0) {
if (components.hour > 1)
strTime = [NSString stringWithFormat:@"%dhours ago", components.hour];
else
strTime = [NSString stringWithFormat:@"%dhour ago", components.hour];
}
else if (components.minute > 0) {
if (components.minute > 1)
strTime = [NSString stringWithFormat:@"%dmin ago", components.minute];
else
strTime = [NSString stringWithFormat:@"%dmin ago", components.minute];
}
else if (components.second > 0) {
if (components.second > 1)
strTime = [NSString stringWithFormat:@"%dsec ago", components.second];
else
strTime = [NSString stringWithFormat:@"%dsec ago", components.second];
}
else {
strTime = @"1 sec ago";
}
return strTime;
}