NSString 到 NSDate,格式为 mm/dd/yyyy hh:mm:ss
我收到的日期字符串是这种格式的 8/5/2011 1:38:13 PM
。如何将其转换为 NSDate
?
我尝试过:
[dateFormatter setDateFormat:@"mm/dd/yyyy 'T' hh:mm:ss a"];
NSString *currentDate = [dateFormatter stringFromDate:currentDateString];
它返回nil
。有什么想法吗?
My date string that I am getting is 8/5/2011 1:38:13 PM
of this format. How do I convert it to NSDate
?
I tried:
[dateFormatter setDateFormat:@"mm/dd/yyyy 'T' hh:mm:ss a"];
NSString *currentDate = [dateFormatter stringFromDate:currentDateString];
It returns nil
. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它返回 nil 的原因是因为您调用了错误的方法,日期字符串中没有
T
(单引号'
中的字符是文字),并且下午不是“下午”。没有破坏它但不完全正确的是你试图将月份解析为分钟,并且你的日期字符串可以至少有 1 位数的月份、日期和小时,所以你不应该使用 2 个说明符来填充它们如果您要根据日期创建一个字符串。试试这个:The reason it returns nil is because you are calling the wrong method, there is no
T
in your date string (characters inside of single quotes'
in are literals), and the PM is not "P.M.". Things that are not breaking it but are not completely correct is you are trying to parse the month as a minute and your date string can have a minimum of 1 digit month, day, and hour so you should not use 2 specifiers which would pad them if you were to create a string from the date. Try this: