NSString *datestr=@"2013-08-13 20:28:40";//Incoming Time//Convert incoming time into desired formatNSDateFormatter *format=[[NSDateFormatter alloc] init]; [Format Setdateformat:@"YYYY-MM-DD HH:mm:ss"]; NSDate*fromdate=[format DATEFROMSTRING:DATESTR]; Nstimezone*fromzone =[Nstimezone Systemtimezone]; Nsinteger Frominterval=[Fromzone secondsfromgmtfordate:fromdate]; NSDate*fromdate =[FromDate Datebyaddingtimeinterval:frominterval]; NSLog (@"fromdate=%@", FromDate); [Format release];//Get current TimeNSDate *date =[NSDate Date]; Nstimezone*zone =[Nstimezone Systemtimezone]; Nsinteger interval=[Zone secondsfromgmtfordate:date]; NSDate*localedate =[Date Datebyaddingtimeinterval:interval]; NSLog (@"enddate=%@", localedate); DoubleIntervalTime = [FromDate timeintervalsincereferencedate]-[Localedate timeintervalsincereferencedate]; LongLtime = (Long) IntervalTime; Nsinteger Iseconds= ltime% -; Nsinteger iminutes= (Ltime/ -) % -; Nsinteger ihours= (Ltime/3600)$; Nsinteger idays= ltime/ -/ -/ -; Nsinteger Imonth= ltime/ -/ -/ -/ A; Nsinteger Iyears= ltime/ -/ -/ -/384; NSLog (@"M-year D-month or D-day D-minute D-Second", iyears,imonth,idays,ihours,iminutes,iseconds);//The code snippet comes from:http://www.sharejs.com/codes/objectc/5099
The string is converted to the NSDate type, which calculates the difference from the current time, the number of years, the number of days, and seconds