NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
NSDate *stutsCreatedDate = [fmt dateFromString:_created_at];
以上是格式化时间格式为指定格式的代码,但是通过运行发现最后一句得到的nsdate永远都是nil ,查询无数文章最终解决。代码修改为
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
[fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *stutsCreatedDate = [fmt dateFromString:_created_at];
本文介绍了一种解决使用NSDateFormatter从特定格式字符串解析日期时返回nil的问题的方法。通过设置NSDateFormatter的locale属性为POSIX英语环境,成功解决了日期解析失败的情况。
1390

被折叠的 条评论
为什么被折叠?



