iOS 开发:NSDateFormatter 问题
我有一个源 @"Tue, 06 Sep 2011 18:32:55 GMT" 字符串,我必须将其解析为 NSDate。
我尝试了以下方法,它在模拟器和设备 <4 iOS 版本上运行良好,但日期对象在 iphone 4 设备上变为空。
NSString *dateString = [NSString stringWithFormat:@"Tue, 06 Sep 2011 18:32:55 GMT"];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *date = [df dateFromString:dateString];
我也尝试过 [df setDateFormat:@"#a, #d #b #Y %H:%M:S #x"],但问题没有解决。
有人有什么想法吗?
I have a source @"Tue, 06 Sep 2011 18:32:55 GMT" string and i have to parse it to NSDate.
i've tried in a following way and it works great on a simulators and devices <4 iOS version, but date object becomes nill on iphone 4 device.
NSString *dateString = [NSString stringWithFormat:@"Tue, 06 Sep 2011 18:32:55 GMT"];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *date = [df dateFromString:dateString];
i've also tried [df setDateFormat:@"#a, #d #b #Y %H:%M:S #x"], but the problem wasnt solved.
does anyone have some ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我唯一的猜测是您的 iOS4 设备没有使用预期的日期格式。
进入您的“设置”-> “一般”-> “国际”并检查所使用的格式是否与其他设备相同。
我仍然觉得奇怪的是,您在示例中对
NSString *dateString
进行了硬编码,但这仍然可能是原因。My only guess is that you are not using the expected date format with your iOS4 Device.
Go into your "Settings" -> "General" -> "International" and check that the format used is the same as the other devices.
What I would still find weird though is that you are hard coding the
NSString *dateString
in your example but still it could be the reason.您是否在日期格式化程序上完成了 setLocale: ?
Have you done a setLocale: on the date formatter?