Calculates the specified time and current difference for example, 3 days ago, 10 minutes ago

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.