简体   繁体   中英

NSDateFormatter returns nil in a simply date format change

I'm trying to convert this date:

Jun 23, 2015 7:53:04 PM

coming from a server response, to a different format using NSDateFormatter but it is constantly returning nil and (null) values in the console if I try to NSLog it. The code I'm using to do so is the following:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"];

NSDate *date = [dateFormatter dateFromString:[contentData valueForKey:@"startDate"]];

Isn't MMM dd, yyyy hh:mm:ss a the correct format to parse the date?

UPDATE

I changed the code in the following way:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"];

NSDate *date = [dateFormatter dateFromString:@"Jun 23, 2015 7:53:04 PM"];

But it still doesn't work

This is working for me.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"];

NSDate *date = [dateFormatter dateFromString:@"Jun 23, 2015 7:53:04 PM"];
NSLog(@"date: %@",date);//date: 2015-06-23 13:53:04 +0000
NSString* formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"Formatted Date: %@",formattedDate);//Formatted Date: Jun 23, 2015 07:53:04 PM

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