IOS obtains the Date value x minutes ago/x hours ago/yesterday/x days ago/x months ago/x years ago

Source: Internet
Author: User

IOS obtains the Date value x minutes ago/x hours ago/yesterday/x days ago/x months ago/x years ago

Returns x minutes ago/x hours ago/yesterday/x days ago/x months ago/x years ago
 

 

-(NSString *) timeInfo {return [NSDate timeInfoWithDate: self];} + (NSString *) timeInfoWithDate :( NSDate *) date {return [self timeInfoWithDateString: [self stringWithDate: date format: [self ymdHmsFormat];} + (NSString *) timeInfoWithDateString :( NSString *) dateString {NSDate * date = [self dateWithString: dateString format: [self ymdHmsFormat]; NSDate * curDate = [NSDate date]; NSTimeInterval time =-[da Te timeIntervalSinceDate: curDate]; int month = (int) ([curDate month]-[date month]); int year = (int) ([curDate year]-[date year]); int day = (int) ([curDate day]-[date day]); NSTimeInterval retTime = 1.0; if (time <3600) {// less than one hour retTime = time/60; retTime = retTime <= 0.0? 1.0: retTime; return [NSString stringWithFormat: @ %. 0f minutes ago, retTime];} else if (time <3600*24) {// less than one day, that is, today's retTime = time/3600; retTime = retTime <= 0.0? 1.0: retTime; return [NSString stringWithFormat: @ %. 0f hours ago, retTime];} else if (time <3600*24*2) {return @ Yesterday;} // The first condition is the same year, and the time is within one month // the second condition is the next year. For the next year, it can only be last December and January this year. In this case, else if (abs (year) = 0 & abs (month) <= 1) | (abs (year) = 1 & [curDate month] = 1 & [date month] = 12) {int retDay = 0; if (year = 0) {// if (month = 0) {// same month retDay = day;} if (retDay <= 0) {// obtain the release date, number of days in a month int totalDays = (int) [self daysInMonth: date month: [date month]; // current number of days + (total number of days in the month of the publication date-date of the month of the publication date, that is, the number of days from today) retDay = (int) [curDate day] + (totalDays-(int) [date day]);} return [NSString stringWithFormat: @ % d days ago, (abs) (retDay)];} else {if (abs (year) <= 1) {if (year = 0) {// return [NSString stringWithFormat: @ % d months ago, abs (month)] ;}// the next year int month = (int) [curDate month]; int preMonth = (int) [date month]; if (month = 12 & preMonth = 12) {// The next year, but in the same month, the return @ 1 years ago is calculated as a full year;} return [NSString stringWithFormat: @ % d months ago, (abs) (12-preMonth + month)];} return [NSString stringWithFormat: @ % d years ago, abs (year)];} return @ 1 hour ago ;}

 

Related Article

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.