IOS calculates how long the last date is from the current code _ios

Source: Internet
Author: User
Tags current time dateformat time interval

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.

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.