Pergunta

I have date string in this format Feb 23, 2014 coming from web services, I want to convert it to this format 02-23-2014, I am trying a lot but all the time nsstring return me null value

NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateStyle:NSDateFormatterMediumStyle];
[DateFormatter setDateFormat:@"MM-dd-yyyy"];

// date formatter two

NSDate *dateVal = (NSDate *)bookDetail.classdate;
NSLog(@"date %@",dateVal);

NSString *dateTwo = [DateFormatter stringFromDate:dateVal];
NSLog(@"new date string %@",dateTwo);
Foi útil?

Solução

NSString *dateString = @"Feb 23, 2014";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top