Calculate escalation Time Difference nsdateformatter *formatter = [[ nsdateformatter alloc] init]; [formatter setdateformat:@ "Yyyy-mm-dd hh:mm:ss"];//set the format you want, the difference between HH and HH: 12-hour, 24-hour nstimezone* timezone = [nstimezone timezonewithname:@ "Asia/Beijing"]; [formatter setTimeZone:timeZone]; NSDate *datenow = [NSDate date]; //set the time of a string nsmutablestring *datestring = [nsmutablestring stringwithformat:@ "%@",20141202052740]; //Note If 20141202052740 must be a number, if it is UNIX time, the following insertion string is not required. [datestring insertstring: @ "-" atindex:4]; [datestring insertstring:@ "-" atindex:7]; [datestring insertstring:@ " " atindex:10]; [datestring insertstring:@ ":" atIndex:13 ]; [datestring insertstring:@ ":" atIndex:16]; nslog (@ "datestring==%@", datestring); NSDateFormatter * dm = [[NSDateFormatter alloc]init]; //Specifies the format of the output here the format must be the same as the format of the above definition string, otherwise the output is null [dm setdateformat:@ "Yyyy-mm-dd hh:mm:ss"]; NSDate * newdate = [dm dateFromString:datestring]; long dd = (Long) [datenow timeintervalsince1970] - [newdate Timeintervalsince1970]; nsstring *[email protected] "" ; if (dd/3600<1) { timestring = [ nsstring stringwithformat:@ "%ld", dd/60]; timestring=[nsstring stringwithformat:@ "%@ minutes Ago", timestring]; } if (dd/3600>1&& DD/86400<1) { timestring = [nsstring stringwithformat:@ "%ld", dd/3600]; timestring=[nsstring stringwithformat:@ "%@ hours Ago", timeString]; } if (dd/86400 >1) { timestring = [nsstring stringwithformat:@ "%ld", dd/86400]; timestring=[nsstring stringwithformat:@ "%@ days Ago" , timestring]; } nslog (@ "=====%@", timestring);
iOS calculation time difference