return x minutes before/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:[s Elf Ymdhmsformat]];} + (NSString *) timeinfowithdatestring: (NSString *) datestring {nsdate *date = [self datewithstring:datestring format:[se LF Ymdhmsformat]]; NSDate *curdate = [NSDate Date]; Nstimeinterval time =-[date 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, i.e. today rettime = time/3600; Rettime = rettime <= 0.0?
1.0:rettime; return [NSString stringwithformat:@ "%.0f hours Ago", rettime]; } else if (Time < 3600 * 2) {return @ "Yesterday"; }//The first condition is the same year, and the time interval is within one months//the second condition is an alternate year, for the alternate year. Can only be last December with this year January such a situation as else if ((ABS (year) = = 0 && abs (month) <= 1) | | (year) = = 1 && [curdate Month] = = 1 && [date Month] = = 12)) {int retday = 0; if (year = = 0) {//if (month = = 0) {//same month retday = day; }} if (Retday <= 0) {//Gets the publication date. How many days of the month int totaldays = (int) [Self daysinmonth:date month:[date Month]]; Current number of Days + (the total number of days in the publication date-month of the publication date, which is equal to the number of days from today) Retday = (int) [Curdate Day] + (Totaldays-(int) [Date]); } return [NSString stringwithformat:@ "%d days Ago", (ABS) (Retday)]; } else {if (ABS (year) <= 1) {if (year = = 0) {//] return [NSString stringwithform at:@"%d months ago", ABS (month)]; }//Alternate year int month = (int) [Curdate month]; int premonth = (int) [Date month]; if (month = = && Premonth = = 12) {//alternate years. But the same month, as a full year to calculate return @ "1 years ago"; } return [NSString stringwithformat:@ "%d months ago", (ABS) (12-premonth + month)]; } return [NSString stringwithformat:@ "%d years ago", ABS (year)]; } return @ "1 hours ago";}
IOS based on date gets X minutes before/x hours ago/yesterday/X days ago/x months ago/x years ago