Voglio due NSInteger anno, mese per tornare un mese indietro. per esempio. ora è 2011-01 come posso ottenere 2010-12. aggiungere è una cosa, ma voglio sottrarre. Spero che qualcuno possa aiutarmi un po 'qui.NSDate sottrarre un mese
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
[calendar release];
NSString * pdf_name = [NSString stringWithFormat:@"file_%d_%02d.pdf", year, month];
saluta endo
EDIT: La mia soluzione
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
BOOL substractOneMonth = YES;
if (substractOneMonth) {
if (month==1) {
year--;
month = 12;
}else {
month--;
}
}
[calendar release];
NSString * pdf_name = [NSString stringWithFormat:@"file_%d_%02d.pdf", year, month];
Scusa quando sto andando con la tua soluzione im ottenere: 4021-12-12 08:32:40 +0000 –
Huh, questo è strano. Ecco la mia uscita: 2011-01-06 03:49:28 +0000 e 2010-12-06 03:49:28 +0000. Mi piacerebbe sentire una spiegazione del perché stiamo vedendo risultati diversi .. – donkim
Questo è il modo corretto per farlo. –