nsstring *distancetime = [self returnfromthetimeoftoday:@ " 2016-09-29 01:45:10 "]; #pragma mark default one minute has 60 seconds, 60 minutes an hour 24 hours a day, 7 days a week, one months 30 days, 12 months a year Do not consider other such as common year February 28 days, leap month 29 days these conditions- (nsstring *) Returnfromthetimeoftoday: (nsstring *) timestr{ //timeStr string Format->2016-09-29 01:45:10 nsdateformatter * form = [[nsdateformatter alloc] init]; //Setting the time zone form.locale = [nslocale localewithlocaleidentifier:@ "cn"]; form.dateformat = @ "Yyyy-mm-dd hh:mm:ss"; nsdate *date = [form datefromstring:timestr]; //Get the current time difference NSTimeInterval timeInterval = [date timeIntervalSinceNow]; timeinterval = -timeinterval; //then the comparison of the time if (timeinterval < 60) { return [nsstring stringwithformat:@ "just" ]; } //min nsinteger minute = timeinterval / 60; if (minute < 60) { return [nsstring stringwithformat:@ "%ld minutes ago", minute ]; } nsinteger hours = minute / 60; if (hours < 24) { return [nsstring stringwithformat:@ "%ld hours ago", hours]; } nsinteger day = hours / 24; nsinteger month = day/30; nsinteger year = month/12; if (day <= &NBSP;1) { form.dateformat = @ "Hh:mm"; nsstring *oldtime = [form stringfromdate:date]; return [nsstring stringwithformat:@ "Yesterday %@", Oldtime]; } if (day < &NBSP;7) { return [NSString stringwithformat:@ "%ld days Ago", Day]; } if ((DAY/7) <4) { return [nsstring stringwithformat:@ "% LD weeks Ago ", Day/7]; } else if (month < 12) { return [nsstring stringwithformat:@ "%ld months ago", month]; } else { form.dateformat = @ "yyyy-mm-dd hh:mm"; return [nsstring stringwithformat:@ "%ld years ago", Year]; } return nil; }
This article is from the "snowflakes flying over the World" blog, please be sure to keep this source http://smengxiang.blog.51cto.com/11204872/1865680
IOS distance now a few hours, minutes, days, weeks, months, years