简体   繁体   English

NSDateFormatter以简单的日期格式更改返回nil

[英]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. 来自服务器的响应,使用NSDateFormatter转换为其他格式,但是如果我尝试NSLog,它将在控制台中不断返回nil(null)值。 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? MMM dd, yyyy hh:mm:ss a解析日期的正确格式吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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