Get time interval iphon

Source: Internet
Author: User

-(Nsstring *) intervalsincenow_yy_mm: (nsdate *) thedate {

Nsdate * edate, * ldate;

Edate = [thedate earlierdate: [nsdate date];
Ldate = [thedate laterdate: [nsdate date];

Nsdateformatter * yydateformater = [[nsdateformatter alloc] init];
[Yydateformater setdateformat: @ "YYYY"];
Nsdateformatter * mmdateformater = [[nsdateformatter alloc] init];
[Mmdateformater setdateformat: @ "mm"];

Nsinteger eyear, emonth, lyear, lmonth, intervalyear, intervalmonth;

Eyear = [[yydateformater stringfromdate: edate] intvalue];
Lyear = [[yydateformater stringfromdate: ldate] intvalue];
Emonth = [[mmdateformater stringfromdate: edate] intvalue];
Lmonth = [[mmdateformater stringfromdate: ldate] intvalue];

If (lmonth> emonth ){
Intervalmonth = lmonth-emonth;
Intervalyear = lyear-eyear;
}
Else if (lmonth <emonth ){
Intervalmonth = 12-emonth + lmonth;
Intervalyear = lyear-eyear-1;
}
Else {
Intervalyear = lyear-eyear;
Return [nsstring stringwithformat: @ "% d Year", intervalyear];
}

Return [nsstring stringwithformat: @ "% d", intervalyear, intervalmonth];

}

 

 

The official API may save some trouble.

Nsdate * begin = [nsdate date];

Unsigned int unitflags =
Nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit |
Nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit;
Nscalendar * Cal = [nscalendar currentcalendar];
Nsdatecomponents * D = [Cal components: unitflags fromdate: Begin todate: [nsdate date] Options: 0];
Int I = 0;
I = [d Second];

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.