将 Ruby on Rails DateTime 转换为 NSDate 时出现问题
为什么这......
NSString *mydate = @"2011-07-20T23:59:00-07:00"
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLog(@"%@", [[dateFormatter dateFromString:mydate] description]);
在控制台中返回“(null)”?
Why is this ...
NSString *mydate = @"2011-07-20T23:59:00-07:00"
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLog(@"%@", [[dateFormatter dateFromString:mydate] description]);
... returning "(null)" in the console?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的日期格式
@"yyyy-MM-dd'T'HH:mm:ss'Z'"
意味着它正在寻找文字“Z
”字符字符串的末尾。但是,您的字符串是:我在那里没有看到“
Z
”,是吗?因此,日期返回为nil
,因为您的字符串与指定的格式不匹配。根据日期格式模式文档,您可能正在寻找格式字符串:
但是,即使这样也可能不起作用,因为如果您发现源字符串在时区的小时和分钟之间有一个冒号(“
:
”)抵消。没有时区说明符可以解释这一点。如果这确实是 ROR 返回日期的格式,则 ROR 是错误的。Your date format of
@"yyyy-MM-dd'T'HH:mm:ss'Z'"
means that it's looking for a literal "Z
" character at the end of the string. However, your string is:I don't see a "
Z
" there, do you? Thus, the date comes back asnil
because your string does not match the format specified.According to the Date Formatting Patterns documentation, you're probably looking for the format string of:
However, even that might not work, because if you notice your source string has a colon ("
:
") in between the hours and minutes of the timezone offset. There's no timezone specifier that accounts for that. If that is indeed the format in which ROR is returning the date, then ROR is wrong.我正在通过 JSON API 将日期从 Ruby 传输到 iOS,我相信我已经阅读了同一篇文章。
http://www.cimgf.com/2012/05 /29/importing-data-made-easy/
我几乎肯定他们错误地添加了“Z”作为字符串文字
-
为了让 Ruby 按照您可能期望的方式输出,我做了以下操作:
请注意 %:z 在 Ruby 1.9.3 中至少可用
Ruby 1.8.7 time.strftime %z 是否存在错误?
I am doing transfer of Dates from Ruby to iOS via a JSON API and I believe I have read the same article.
http://www.cimgf.com/2012/05/29/importing-data-made-easy/
I am almost positive that they have mistakenly added the 'Z' as a string literal
-
To get Ruby to output as you are probably expecting I did the following:
Notice the %:z that is available in Ruby 1.9.3 at least
Is there a Ruby 1.8.7 time.strftime %z bug?