Question

Je veux deux années NSInteger, mois pour aller un mois en arrière. par exemple. est maintenant 2011-01 Comment puis-je obtenir 2010-12 . l'ajout est une chose, mais je veux soustraire.

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

endo salue

EDIT: Ma solution

    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];
Était-ce utile?

La solution

Ce code de l'échantillon est de date de l'iPhone et de l'heure Guide de programmation.

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

J'espère que cela aide!

Autres conseils

Alors que la réponse acceptée fonctionne, il utilise le code désapprouvée dans iOS 8.0. Une autre façon de faire la même chose sans utiliser le code désapprouvée se présente comme suit:

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

Je suggère la création d'une extension à cet effet:

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)
    }

}

Utilisation:

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];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top