2011-01-06 21 views
19

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

risposta

57

Questo codice di esempio è da Guida alla programmazione di iPhone Data e ora.

NSDate *today = [[NSDate alloc] init]; 
NSLog(@"%@", today); 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; 
[offsetComponents setMonth:-1]; // note that I'm setting it to -1 
NSDate *endOfWorldWar3 = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0]; 
NSLog(@"%@", endOfWorldWar3); 

Spero che questo aiuti!

+0

Scusa quando sto andando con la tua soluzione im ottenere: 4021-12-12 08:32:40 +0000 –

+0

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

+0

Questo è il modo corretto per farlo. –

1
NSDate *date = [NSDate date]; 
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date]; 
NSInteger year = [dateComponents year]; 
NSInteger month = [dateComponents month]; 

month -= month - 1; 

if (month < 1) { 
year -= 1; 
month = 12; 
} 

[calendar release]; 
NSString * pdf_name = [NSString stringWithFormat:@"file_%d_%02d.pdf", year, month]; 
+0

Np! Modifica: mese fisso in caso limite – Penang

5

Mentre la risposta accettata funziona, utilizza codice ammortizzato in iOS 8.0. Un altro modo per fare la stessa cosa senza utilizzare il codice deprecato è il seguente:

NSDate *date = [NSDate date]; 
NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
[dateComponents setMonth:-1]; 
date = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0]; 
3

Suggerisco di creare un'estensione per questo scopo:

extension Date { 

    func adding(months: Int) -> Date? { 
     let calendar = Calendar(identifier: .gregorian) 

     var components = DateComponents() 
     components.calendar = calendar 
     components.timeZone = TimeZone(secondsFromGMT: 0) 
     components.month = months 

     return calendar.date(byAdding: components, to: self) 
    } 

} 

Usage:

let now = Date() 
let sixMonthsBefore = now.adding(months: -6) 
let threeMonthsAfter = now.adding(months: 3)