NSDate de cadena

Tengo una cadena "2012-09-16 23:59:59 JST" Quiero convertir esta cadena de date en NSDate.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"]; NSDate *captunetworkingStartDate = [dateFormatter dateFromString: @"2012-09-16 23:59:59 JST"]; NSLog(@"%@", captunetworkingStartDate); 

Pero no está funcionando. Es un valor nulo. Por favor ayuda..

Cuando se usa el time de 24 horas, el especificador de horas debe ser un H capital como este:

 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; 

Marque aquí los especificadores correctos: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Sin embargo, debe establecer la configuration regional para el formateador de date:

 // Set the locale as needed in the formatter (this example uses Japanese) [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]]; 

Código completo de trabajo:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"]; [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]]; NSDate *captunetworkingStartDate = [dateFormatter dateFromString: @"2012-09-16 23:59:59 JST"]; NSLog(@"Captunetworking Date %@", [captunetworkingStartDate description]); 

Salidas (en GMT):

 Captunetworking Date 2012-09-16 14:59:59 +0000 
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; NSDate *captunetworkingStartDate = [dateFormatter dateFromString: @"2012-09-16 23:59:59 GMT-08:00"]; NSLog(@"%@", captunetworkingStartDate); 
 NSString *dateString = @"01-02-2010"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // this is imporant - we set our input date format to match our input string // if format doesn't match you'll get nil from your string, so be careful [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSDate *dateFromString = [[NSDate alloc] init]; // end dateFromString = [dateFormatter dateFromString:dateString]; [dateFormatter release];