iOS is often used to show code snippets a few hours ago/days ago/months ago/years ago2015-03-13 10:09
copyright Notice: Please pay attention to personal blog: http://www.henishuo.com/
print?
- /**
- * Retain a formated string with a real date string
- *
- * @param datestring a real date string, which can be converted to a NSDate object
- *
- * @return A string that'll be x minutes ago/x hours ago/yesterday/X days ago/x months ago/x years ago
- */
- + (NSString *) timeinfowithdatestring: (NSString *) datestring {
- //Format the date string as a Date object
- nsdate *date = [nsdate datefromstring:datestring withformat:@ "Yyyy-mm-dd HH:mm:ss"];
- nsdate *curdate = [nsdate Date];
- Nstimeinterval time =-[date timeintervalsincedate:curdate];
- int month = (int) ([curdate getMonth]-[date getMonth]);
- int year = (int) ([curdate getYear]-[date getYear]);
- int day = (int) ([curdate GetDay]-[date GetDay]);
- Nstimeinterval rettime = 1. 0;
- //Less than one hour
- if (Time < 3600) {
- Rettime = time/ 60;
- Rettime = Rettime <= 0. 0? 1. 0:rettime;
- return [NSString stringWithFormat:@ "%.0f minutes ago", Rettime];
- }
- //Less than one day, i.e. today
- Else if (Time < 33600 * 24) {
- Rettime = time/ 3600;
- Rettime = Rettime <= 0. 0? 1. 0:rettime;
- return [NSString stringWithFormat:@ "%.0f hours ago", Rettime];
- }
- //Yesterday
- Else if (Time < 33600 * 2* 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 next year, only last December with this year January this situation
- Else if ((ABS (year) = = 0 && abs (month) <= 1)
- || (year) = = 1 && [curdate getMonth] = = 1 && [date getMonth] = = 12)) { /c10>
- int retday = 0;
- //Same year
- if (year = = 0) {
- //Same month
- if (month = = 0) {
- Retday = day;
- }
- }
- if (retday <= 0) {
- //This is calculated by the maximum monthly value.
- //Gets the release date, the total number of days in the month
- int totaldays = [NSDate daysinmonth: (int) [Date GetMonth] year : (int) [date getYear]];
- //Current days + (the total number of days in the release date-month of the release date, which is equal to the number of days from today)
- Retday = (int) [Curdate GetDay] + (Totaldays-(int) [date GetDay]);
- if (retday >= totaldays) {
- return [NSString stringWithFormat:@ "%d months ago", (ABS) (MAX (Retday/ 31, 1))];
- }
- }
- return [NSString stringWithFormat:@ "%d days ago", (ABS) (Retday)];
- } Else {
- if (ABS (year) <= 1) {
- if (year = = 0) { //
- return [NSString stringWithFormat:@ "%d months ago", ABS (month)];
- }
- //difference of one year
- int month = (int) [curdate getMonth];
- int premonth = (int) [date getMonth];
- //Every other year, but the same month, as a full year to calculate
- if (month = = 12 && premonth = = 12) {
- return @ "1 years ago";
- }
- //Nor look, but not the same month
- return [NSString stringWithFormat:@ "%d months ago", (ABS) (12-premonth + month)];
- }
- return [NSString stringWithFormat:@ "%d years ago", ABS (year)];
- }
- return @ "1 hours ago";
- }
The number of months before this calculation, in order to reduce the amount of calculation, not to get the total number of days for the corresponding month, but the month maximum value of 31 as the standard, therefore,
If you need more accurate calculations, replace the corresponding small piece of code
iOS is often used to show code snippets a few hours ago/days ago/months ago/years ago