将字符串转换为 NSDate 问题
我一直在查看 StackOverflow,但还没有找到任何答案,如果我错过了回答这个问题的帖子,我深表歉意,并将不胜感激。
我正在尝试将字符串“prod.Start”更改为 NSDate 类型,以便与今天的日期进行比较。以下代码 myDate 返回“1753-01-01 00:00:00 -075258”
代码:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setFormatterBehavior:NSDateFormatterBehavior10_4];
[format setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *myDate = [format dateFromString: prod.Start]; //prod.Start = 1/1/1753 12:00:00 AM
任何建议/提示将不胜感激。
谢谢
编辑:
现在可以使用:感谢您的所有帮助。 我认为错误是上午 12 点 = 上午 0 点,
我尝试中午 12 点,输出是“1753-01-01 12:00:00 -075258”,
谢谢您还解释了“-075258”= PST 对此感到好奇。
I have been looking over StackOverflow and have not found any answers yet, if I missed a post which answers this I apologize and would be grateful for the link.
I am trying to change a string "prod.Start" into a NSDate type for comparison with today's date. The following code myDate returns "1753-01-01 00:00:00 -075258"
CODE:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setFormatterBehavior:NSDateFormatterBehavior10_4];
[format setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *myDate = [format dateFromString: prod.Start]; //prod.Start = 1/1/1753 12:00:00 AM
Any suggestions/tips would be appreciated.
Thanks
edit:
Works now : thanks for all the help.
What I thought was an error was 12AM = 0 o'clock AM
I tried 12PM and output was "1753-01-01 12:00:00 -075258"
Thank you for also explaining the "-075258" = PST was curious about that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你走在正确的轨道上。 NSDate 比较函数仅适用于该标准格式,因此这是正确的输出(尽管如果我的代码不起作用,请检查 Max 的解决方案)。您现在需要将其与今天的日期进行比较,如下所示...
这应该会为您提供所需的内容。
You're on the right track. The NSDate compare functions only work with that standard format, so that is the right output (though if my code doesn't work, check Max's solution). You now need to compare this to today's date like so...
That should give you what you're looking for.
您使用的是 10.4 样式格式字符串,但默认格式化程序行为是 10.0 样式。您确定您的格式化程序使用了正确的样式吗?您可以使用
–setFormatterBehavior:
或全局使用+setDefaultFormatterBehavior:
。You're using a 10.4-style format string, but the default formatter behavior is 10.0-style. Did you make sure that your formatter is using the right style? You can change it with
–setFormatterBehavior:
or globally with+setDefaultFormatterBehavior:
.