-(NSString*)timeAgoFor:(NSString*)tipping_date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:tipping_date];
NSString *key = @"";
NSTimeInterval ti = [date timeIntervalSinceDate:[NSDate date]];
key = (ti > 0) ? @"Left" : @"Ago";
ti = ABS(ti);
NSDate * today = [NSDate date];
NSComparisonResult result = [today compare:date];
if (result == NSOrderedSame) {
return[NSString stringWithFormat:@"Today"];
}
else if (ti < 86400 * 2) {
return[NSString stringWithFormat:@"1 Day %@",key];
}else if (ti < 86400 * 7) {
int diff = round(ti/60/60/24);
return[NSString stringWithFormat:@"%d Days %@", diff,key];
}else {
int diff = round(ti/(86400 * 7));
return[NSString stringWithFormat:@"%d Wks %@", diff,key];
}
}
प्रत्येक केस स्टेटमेंट के बाद ब्रेक होना चाहिए ताकि अन्य मामलों में प्रवेश न किया जा सके। –
ध्यान दें कि यह कभी भी "आज" वापस नहीं आएगा - एनएसडीएटी समय में एक विशिष्ट तत्काल का प्रतिनिधित्व करता है, इसलिए 'NSOrderedSame' (अनिवार्य रूप से) कभी नहीं होगा – Tim