iphone objective-c nsdate nsdatecomponents

iphone - NSDate restar un mes



objective-c nsdatecomponents (4)

Quiero dos años de NSInteger, un mes para volver un mes atrás. por ejemplo, ahora es 2011-01, ¿cómo puedo obtener 2010-12 . Agregar es una cosa, pero quiero restar.

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

saluda endo

EDITAR: Mi solución

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


Este código de ejemplo es de la Guía de programación de fecha y hora del iPhone.

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

¡Espero que esto ayude!


Mientras que la respuesta aceptada funciona, utiliza el código depreciado en iOS 8.0. Otra forma de hacer lo mismo sin usar código en desuso es la siguiente:

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


Sugiero crear una extensión para este propósito:

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

Uso:

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