将字符串转换为 NSDate 的正确方法?(iphone)

发布于 2024-08-19 10:33:45 字数 223 浏览 2 评论 0原文

在我的应用程序中,我将日期存储为字符串(“MM/dd/YYYY”格式)。在数据库中。稍后,当我检索字符串时,我必须比较两个日期,如何实现这一目标?

如果我使用“YYYY/MM/dd”格式,我可以直接将其作为字符串进行比较。 现在我必须使用“nsdateformatter”和“dateFromString”转换回 NSDate 对象。但无论我做什么,我都会使用它得到一个恒定的日期值。

有什么想法吗?

In my app I store the date as a string("MM/dd/YYYY" format). In the DB. later when I retrieve the string I have to compare two dates, how do I achieve this?

If I had used the"YYYY/MM/dd" format I could have directly compared it as strings.
Now I have to convert back into NSDate object using "nsdateformatter" and "dateFromString". But I get a constant date value using this no matter what I do.

Any ideas??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

沩ん囻菔务 2024-08-26 10:33:45
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* d = [df dateFromString:@"12/23/2046"];
NSLog(@"%@", d);

结果:

2046-12-23 00:00:00 +0800
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* d = [df dateFromString:@"12/23/2046"];
NSLog(@"%@", d);

Result:

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