There are several posting time formats available, social sites are using their own styles of post time display. But the logic behind each posting is same, basically it depnds upon the date and time difference. Here is the function to calculate the difference.
This method returns String, you just need to pass NSDate as a parameter-
-(NSString *)dateDiff:(NSDate *)origDate {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *todayDate = [NSDate date];
double ti = [origDate timeIntervalSinceDate:todayDate];
ti = ti * -1;
if(ti < 1) {
return @"few seconds ago";
} else if (ti < 60) {
return @"less than a minute ago";
} else if (ti < 3600) {
int diff = round(ti / 60);
return [NSString stringWithFormat:@"%d minutes ago", diff];
} else if (ti < 86400) {
int diff = round(ti / 60 / 60);
return[NSString stringWithFormat:@"%d hours ago", diff];
} else if (ti < 2629743) {
int diff = round(ti / 60 / 60 / 24);
return[NSString stringWithFormat:@"%d days ago", diff];
} else {
return @"never";
}
}
Happy Coding!!!
0 Comment(s)