IOS obtains the Date value x minutes ago/x hours ago/yesterday/x days ago/x months ago/x years ago
Returns x minutes ago/x hours ago/yesterday/x days ago/x months ago/x years ago
-(NSString *) timeInfo {return [NSDate timeInfoWithDate: self];} + (NSString *) timeInfoWithDate :( NSDate *) date {return [self timeInfoWithDateString: [self stringWithDate: date format: [self ymdHmsFormat];} + (NSString *) timeInfoWithDateString :( NSString *) dateString {NSDate * date = [self dateWithString: dateString format: [self ymdHmsFormat]; NSDate * curDate = [NSDate date]; NSTimeInterval time =-[da Te timeIntervalSinceDate: curDate]; int month = (int) ([curDate month]-[date month]); int year = (int) ([curDate year]-[date year]); int day = (int) ([curDate day]-[date day]); NSTimeInterval retTime = 1.0; if (time <3600) {// less than one hour retTime = time/60; retTime = retTime <= 0.0? 1.0: retTime; return [NSString stringWithFormat: @ %. 0f minutes ago, retTime];} else if (time <3600*24) {// less than one day, that is, today's retTime = time/3600; retTime = retTime <= 0.0? 1.0: retTime; return [NSString stringWithFormat: @ %. 0f hours ago, retTime];} else if (time <3600*24*2) {return @ Yesterday;} // The first condition is the same year, and the time is within one month // the second condition is the next year. For the next year, it can only be last December and January this year. In this case, else if (abs (year) = 0 & abs (month) <= 1) | (abs (year) = 1 & [curDate month] = 1 & [date month] = 12) {int retDay = 0; if (year = 0) {// if (month = 0) {// same month retDay = day;} if (retDay <= 0) {// obtain the release date, number of days in a month int totalDays = (int) [self daysInMonth: date month: [date month]; // current number of days + (total number of days in the month of the publication date-date of the month of the publication date, that is, the number of days from today) retDay = (int) [curDate day] + (totalDays-(int) [date day]);} return [NSString stringWithFormat: @ % d days ago, (abs) (retDay)];} else {if (abs (year) <= 1) {if (year = 0) {// return [NSString stringWithFormat: @ % d months ago, abs (month)] ;}// the next year int month = (int) [curDate month]; int preMonth = (int) [date month]; if (month = 12 & preMonth = 12) {// The next year, but in the same month, the return @ 1 years ago is calculated as a full year;} return [NSString stringWithFormat: @ % d months ago, (abs) (12-preMonth + month)];} return [NSString stringWithFormat: @ % d years ago, abs (year)];} return @ 1 hour ago ;}