In the open-source Chinese client, whether it is a comprehensive article, a question in the Q & A, or a moving talk, each article will be followed by a comment with time, for example, "10 minutes ago", "2 hours ago", "5 days ago", or "-2-9", these times are mandatory in many applications, in order to give the user a sense of time, know that the information is the latest, and also convenient to view the previous information based on time;
The time format parsed from the API is yyyy-MM-dd HH: mm: ss (for example, 09:51:22 ), after obtaining the article time on the API, it is obtained through algorithm conversion and the Tool of the client source code. m + (NSString *) intervalSinceNow: (NSString *) theDate to implement this method: if the information is displayed for minutes before 1 hour, the information within one day is displayed for hours, display by day within 10 days, and display by posting time outside 10 days: [cpp] + (NSString *) intervalSinceNow: (NSString *) theDate {NSDateFormatter * date = [[NSDateFormatter alloc] init]; [date setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; NSDate * d = [date dateFromString: theDate]; NSTimeInterval late = [d timeIntervalSince1970] * 1; NSDate * dat = [NSDate dateWithTimeIntervalSinceNow: 0]; NSTimeInterval now = [dat timeIntervalSince1970] * 1; NSString * timeString = @ ""; NSTimeInterval cha = now-late; // post within one hour if (cha/3600 <1) {if (cha/60 <1) {timeString = @ "1";} else {timeString = [NSString stringWithFormat: @ "% f", cha/60]; timeString = [timeString substringToIndex: timeString. length-7];} timeString = [NSString stringWithFormat: @ "% @ Minutes Ago", timeString];} // else if (cha/3600> 1 & cha/86400 <1) {timeString = [NSString stringWithFormat: @ "% f ", cha/3600]; timeString = [timeString substringToIndex: timeString. length-7]; timeString = [NSString stringWithFormat: @ "% @ hour before", timeString];} // else if (cha/86400> 1 & cha/864000 <1) {timeString = [NSString stringWithFormat: @ "% f ", cha/86400]; timeString = [timeString substringToIndex: timeString. length-7]; timeString = [NSString stringWithFormat: @ "% @ days ago", timeString];} // The posting time is greater than 10 days. else {// timeString = [NSString stringWithFormat: @ "% d-%"] NSArray * array = [theDate componentsSeparatedByString: @ ""]; // return [array objectAtIndex: 0]; timeString = [array objectAtIndex: 0];} return timeString;} This code can be used directly in your own project. The returned timeString is the time period required;