Pregunta

tengo un NSString como esto 05/09/2015 entonces quiero convertirlo a este formato 2014-07-28 18:30:00 +0000

esa segunda fecha fue la que obtengo de la fecha del sistema.Esa fecha se puede ubicar muy bien en Tapku calandrar.pero mi cadena es la primera.Quiero convertir esa primera fecha tal como el formato de fecha que obtengo de la fecha del sistema.Cómo puedo hacer eso.

Por favor, ayúdame.Gracias

¿Fue útil?

Solución

NSString a NSDate

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];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];

NSDateFormatter *otherFormatter = [[NSDateFormatter alloc] init];
[otherFormatter setDateFormat:@"yyyy-MM-dd"];
//[otherFormatter setTimeStyle:NSDateFormatterMediumStyle];

NSString *resultString = [otherFormatter stringFromDate:dateFromString];
NSLog(@"%@", resultString);

Y luego puede convertirlo nuevamente a NSDate o conservarlo como NSString.

Mira esto: Haga clic aquí

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top