ios - NSDateFormatter 在 iOS5.0、iOS6.0 中返回 null

标签 ios iphone nsdate nsdateformatter

我正在从服务器获取一个字符串,并将其转换为日期。以下是我正在使用的代码。

NSString *endDateString = @"2014-03-01T00:00:00-08:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *eventEndDate = [dateFormatter dateFromString:endDateString];
NSLog(@"%@", eventEndDate);

它在 iOS7.0 中运行良好,但在 iOS 5.0 和 6.0 中给我一个 NULL。

最佳答案

带有嵌入“:”的时间轴偏移的日期格式错误。

使用:

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];

来自:ICU Formatting Dates and Times

enter image description here

关于ios - NSDateFormatter 在 iOS5.0、iOS6.0 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21168774/

相关文章:

ios - NSTimer 不够精确

iOS iCloud 容器 : are these two containers the same?

ios - 尝试在 View 不在窗口层次结构中的 UITabBarController 上显示 UIAlertController

iphone - Paypal 错误: FILE SYSTEM CHECK FAILED

iphone - 无法使用 NSDateFormatter 格式化 UTC 日期

iphone - 格式化字符串中的日期

iphone - 将另一个 View 附加到同一 View "files owner"

ios - 在 IOS 中无法从 Google Drive 获取文件

iphone - 如何在 iOS 中为应用程序图标制作动画?

iphone - 在 AlertView OK 按钮声明中执行一段代码