将字符串转换为 NSDate 问题

发布于 2024-09-16 23:33:15 字数 648 浏览 5 评论 0原文

我一直在查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧 2024-09-23 23:33:15

你走在正确的轨道上。 NSDate 比较函数仅适用于该标准格式,因此这是正确的输出(尽管如果我的代码不起作用,请检查 Max 的解决方案)。您现在需要将其与今天的日期进行比较,如下所示...

NSDate *todaysDate = [NSDate date];
BOOL isToday = [todaysDate isEqualToDate:myDate];

这应该会为您提供所需的内容。

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...

NSDate *todaysDate = [NSDate date];
BOOL isToday = [todaysDate isEqualToDate:myDate];

That should give you what you're looking for.

暖树树初阳… 2024-09-23 23:33:15

您使用的是 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:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文