Calculates the specified time and the current difference, for example, 3 days ago, 10 minutes ago (this is often encountered in the project, so recorded)
Here's how it's implemented:
/**
* Calculates the specified time and the current difference
* @param comparedate A specified time
* @return How much ( seconds or minutes or days or months or years) + before ( e.g. 3 days ago, ten minutes ago)
*/
+ (NSString *) Comparecurrenttime: (nsdate*) comparedate
//
{
nstimeinterval timeinterval = [comparedate timeintervalsincenow];
TimeInterval =-timeinterval;
Long temp = 0;
nsstring *result;
if (TimeInterval < ) {
result = [nsstringstringWithFormat:@ " just "];
}
Else if ((temp = timeinterval/) <) {
result = [nsstringstringWithFormat:@ "%d minutes ago ", temp];
}
Else if ((temp = temp/) <) {
result = [nsstringstringWithFormat:@ "%d small front ", temp];
}
Else if ((temp = temp/) <) {
result = [nsstringstringWithFormat:@ "%d days ago ", temp];
}
Else if ((temp = temp/) <) {
result = [nsstringstringWithFormat:@ "%d months ago ", temp];
}
else{
temp = temp/;
result = [nsstringstringWithFormat:@ "%d years ago ", temp];
}
return result;
}
The following are common methods in NSDate:
/**
-(ID) Initwithtimeinterval: (nstimeinterval) secs sincedate: (NSDate *) refdate;
Initialized to refdate, then secs seconds later.
-(ID) Initwithtimeintervalsincenow: (nstimeinterval) secs;
Initialized to the current time, and then secs seconds later
-(Nstimeinterval) Timeintervalsincedate: (NSDate *) refdate;
Returns the time interval between the time the instance was saved and the refdate , with Refdate as the base time
-(Nstimeinterval) Timeintervalsincenow;
Returns the time between the time that the instance was saved and the current time ( now ) at the current time (now)
-(Nstimeinterval) timeIntervalSince1970;
returns the time between the time the instance was saved and 1970/01/01 GMT, with 1970/01/01 GMT as the base time
-(Nstimeinterval) timeintervalsincereferencedate;
returns the time between the time the instance was saved and 2001/01/01 GMT, with 2001/01/01 GMT as the base time
+ (Nstimeinterval) timeintervalsincereferencedate;
*/
// sec
-(Nstimeinterval) Timeintervalsincenow;
Returns the time between the time that the instance was saved and the current time ( now ) at the current time (now)
Calculates the specified time and current difference for example, 3 days ago, 10 minutes ago