-(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];