简体   繁体   中英

how to convert nsdate to another format using nsdateformatter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM