This article is an example of how the iOS last date distance from the current calculation code for your reference, the details are as follows
/** * Calculate Last Date distance now * * @param lasttime Last date (required and format) * @param format1 Last date format * @param currenttime Recent date (required and format)
* @param FORMAT2 Recent date format * * @return xx minutes ago, xx hours ago, xx days ago * * * + (NSString *) Timeintervalfromlasttime: (NSString *) lasttime Lasttimeformat: (NSString *) format1 tocurrenttime: (NSString *) currenttime currenttimefor
Mat: (NSString *) format2{//last time NSDateFormatter *dateformatter1 = [[NSDateFormatter alloc]init];
Dateformatter1.dateformat = FORMAT1;
NSDate *lastdate = [DateFormatter1 datefromstring:lasttime];
Current time NSDateFormatter *dateformatter2 = [[NSDateFormatter alloc]init];
Dateformatter2.dateformat = FORMAT2;
NSDate *currentdate = [DateFormatter2 datefromstring:currenttime];
return [Utilities timeintervalfromlasttime:lastdate tocurrenttime:currentdate]; } + (NSString *) Timeintervalfromlasttime: (NSDate *) lasttime tocurrenttime: (nsdate *) currenttime{NSTimeZone
= [Nstimezone Systemtimezone]; Last time NSdate *lastdate = [Lasttime datebyaddingtimeinterval:[timezone secondsfromgmtfordate:lasttime]];
Current time NSDate *currentdate = [currenttime datebyaddingtimeinterval:[timezone secondsfromgmtfordate:currenttime]]; Time interval Nsinteger intevaltime = [currentdate timeintervalsincereferencedate]-[Lastdate timeintervalsincereferencedate
];
Seconds, minutes, hours, days, months, years nsinteger minutes = INTEVALTIME/60;
Nsinteger hours = INTEVALTIME/60/60;
Nsinteger day = intevaltime/60/60/24;
Nsinteger month = INTEVALTIME/60/60/24/30;
Nsinteger yers = intevaltime/60/60/24/365;
if (minutes <=) {return @ "just";
}else if (minutes <) {return [NSString stringWithFormat: @ '%ld minutes ago ', (long) minutes];
}else if (Hours <) {return [NSString stringWithFormat: @ "%ld hours ago", (long) hours];
}else if (Day <) {return [NSString stringWithFormat: @ "%ld days Ago", (long)];
}else if (Month <) {NSDateFormatter * df =[[nsdateformatter Alloc]init]; Df.dateformat = @ "M month D Day";
NSString * time = [DF stringfromdate:lastdate];
return time;
}else if (yers >= 1) {NSDateFormatter * df =[[nsdateformatter Alloc]init];
Df.dateformat = @ "yyyy year M month D Day";
NSString * time = [DF stringfromdate:lastdate];
return time;
} return @ "";
}
Use the following:
NSLog (@ "\n\nresult:%@", [Utilities timeintervalfromlasttime:@ "December 8, 2015 15:50" lasttimeformat:@ "yyyy
year mm month DD Day hh:mm "
tocurrenttime:@" 2015/12/08 16:12 "currenttimeformat:@" Yyyy/mm/dd "
]);
The output results are as follows:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.