IOS based on date gets X minutes before/x hours ago/yesterday/X days ago/x months ago/x years ago

Source: Internet
Author: User

return x minutes before/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:[s Elf Ymdhmsformat]];} + (NSString *) timeinfowithdatestring: (NSString *) datestring {nsdate *date = [self datewithstring:datestring format:[se        LF Ymdhmsformat]];    NSDate *curdate = [NSDate Date];        Nstimeinterval time =-[date 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, i.e. today rettime = time/3600; Rettime = rettime <= 0.0?

1.0:rettime; return [NSString stringwithformat:@ "%.0f hours Ago", rettime]; } else if (Time < 3600 * 2) {return @ "Yesterday"; }//The first condition is the same year, and the time interval is within one months//the second condition is an alternate year, for the alternate year. Can only be last December with this year January such a situation as else if ((ABS (year) = = 0 && abs (month) <= 1) | | (year) = = 1 && [curdate Month] = = 1 && [date Month] = = 12)) {int retday = 0; if (year = = 0) {//if (month = = 0) {//same month retday = day; }} if (Retday <= 0) {//Gets the publication date. How many days of the month int totaldays = (int) [Self daysinmonth:date month:[date Month]]; Current number of Days + (the total number of days in the publication date-month of the publication date, which is equal to the number of days from today) Retday = (int) [Curdate Day] + (Totaldays-(int) [Date]); } return [NSString stringwithformat:@ "%d days Ago", (ABS) (Retday)]; } else {if (ABS (year) <= 1) {if (year = = 0) {//] return [NSString stringwithform at:@"%d months ago", ABS (month)]; }//Alternate year int month = (int) [Curdate month]; int premonth = (int) [Date month]; if (month = = && Premonth = = 12) {//alternate years. But the same month, as a full year to calculate return @ "1 years ago"; } return [NSString stringwithformat:@ "%d months ago", (ABS) (12-premonth + month)]; } return [NSString stringwithformat:@ "%d years ago", ABS (year)]; } return @ "1 hours ago";}




IOS based on date gets X minutes before/x hours ago/yesterday/X days ago/x months ago/x years 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.