Cómo restar 30 minutos de una date

Estoy recibiendo la date de valor de date: 2013-06-07 18:30:00 +0000. Estoy tratando de restar 30 minutos de esto, pero no da el time apropiado.

He probado esto

NSTimeInterval secondsPerHour = 60*30; NSDate *newDate = [date dateByAddingTimeInterval:-secondsPerHour]; 

Motivo : El negative de un unsigned int es un positive number . Esto debería funcionar:

 NSDate *newDate = [date dateByAddingTimeInterval:-(NSTimeInterval)((u_int32_t)30*60)]; 

EDITAR : uso alternativo NSDateComponents .

 NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setMinute:-30]; NSDate *halfHourAgo = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:yourDateHere options:0]; NSLog(@"\ncurrentDate: %@ \n halfHourAgo: %@", yourDateHere, halfHourAgo); 

puedes usar:

 NSDate *minusOneHr = [[NSDate date] dateByAddingTimeInterval:-30*60]; 

Podrías probar NSDateComponents , algo así:

 NSDate* yourDate = [NSDate dateWithTimeIntervalSinceNow:0]; // your date, to substract 30 mins NSDateComponents *dc = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:yourDate]; [dc setMinute:[dc minute] - 30]; NSDate* date = [[NSCalendar currentCalendar] dateFromComponents:dc]; // 30 mins before now 

O eso:

 NSDate* yourDate = [NSDate dateWithTimeIntervalSinceNow:0]; // your date, to substract 30 mins NSDateComponents *components = [[NSDateComponents alloc] init]; [components setMinute:-30]; NSDate* date = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:yourDate options:0]; // 30 mins before now 

Copia pega este código en viewDidLoad y verifica primero los resultados.

 NSDate *today = [[NSDate alloc] init]; NSLog(@"Today %@", today); NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setMinute:-30]; // If you want to change Month,Year,Day,Hour etc. Just change the "setMinute" to setMonth,setYear,setDay,setHour NSDate *dateComp = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0]; NSLog(@"Desinetworking Day %@", dateComp);