Domanda

Voglio due NSInteger anno, il mese di andare un mese indietro. per esempio. ora è 2011-01 come posso ottenere 2010-12 . l'aggiunta è una cosa, ma voglio sottrarre.

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];
È stato utile?

Soluzione

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

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 aiuta!

Altri suggerimenti

Mentre la risposta accettata funziona, utilizza il codice deprecato 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];

I suggeriscono la creazione di 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)
    }

}

Utilizzo:

let now = Date()
let sixMonthsBefore = now.adding(months: -6)
let threeMonthsAfter = now.adding(months: 3)
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];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top