iOS 中解析 ISO8601 时间的问题
我正在尝试将此字符串“2011-11-23T17:59:00Z”转换为 NSDate。
我见过很多人都有这个问题,但每个人的格式都略有不同。我一直无法破解解决方案。
我尝试过如下代码:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.timeStyle = NSDateFormatterFullStyle;
[dateFormat setDateFormat: @"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSString* date = ""2011-11-23T17:59:00Z"";
NSString *dateString = [dateFormat stringFromDate: date];
dateString 总是返回 NULL
I'm trying to convert this string "2011-11-23T17:59:00Z" to an NSDate.
I've seen many people have this problem, but everyone has a slightly different format. I haven't been able to hack a solution.
I've tried code such as:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.timeStyle = NSDateFormatterFullStyle;
[dateFormat setDateFormat: @"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSString* date = ""2011-11-23T17:59:00Z"";
NSString *dateString = [dateFormat stringFromDate: date];
dateString always comes back NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSDateFormatter stringFromDate 接受一个 NSDate 对象,并且您向它传递一个字符串。
http://开发人员。 apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
尝试NSDateFormatter dateFromString。
如果您使用 dateFromString:
输出:
NSDateFormatter stringFromDate takes an NSDate object and you're passing it a string.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Try NSDateFormatter dateFromString.
If you use dateFromString:
Outputs: