在 Objective-C 中将日期字符串格式化为 NSDate

发布于 2024-10-16 16:58:17 字数 475 浏览 6 评论 0原文

我一直在与一些烦人的日期格式作斗争,API 在我的应用程序中返回了我。我需要将以下两种类型的日期转换为可以在我的应用程序中使用的 NSDate。

  • 星期六上午 8 点
  • 星期一晚上 8:30

目前我正在使用与此类似的东西...

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"eee ha"];
NSDate *date = [formatter dateFromString:dateString];

这有两个问题...

  • 是我需要管理的一些时区内容)
  • 它似乎将时间倒退 1 小时(这 无法处理周一 8:30PM 日期...

我非常感谢您提供一些帮助!非常感谢:-)

I've been fighting with some annoying date formats an API has been returning me in my app. I need to convert the following two types of dates into an NSDate I can use around my app.

  • SAT 8AM
  • MON 8:30PM

At the moment I'm using something similar to this...

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"eee ha"];
NSDate *date = [formatter dateFromString:dateString];

There are two problems with this...

  • It seems to put the time back 1 hour (is this some timezone stuff I need to manage)
  • It can't handle the MON 8:30PM dates...

I'd really appreciate some help with this! Thanks a bunch :-)

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

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

发布评论

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

评论(3

提赋 2024-10-23 16:58:17

正如 dreamlax 指出的那样,如果您希望它支持多种日期格式,您将必须使用每种格式来调用它,直到一种匹配为止。

至于时区不匹配,格式化程序的时区默认为系统的当前时区。你能展示一些代码来演示它是如何减少一个小时的吗?我没有看到这样的事情。

当然,这也是一件很奇怪的事情。您确实知道“SAT 8AM”的意思是“参考日期后第一个星期六的当地时间 8 点”,对吗?所以这应该是 1970 年 1 月 3 日。它并不意味着“今天之后的下一个星期六”或类似的意思。我可以想象一些奇怪的情况,这会很有用,但我想确保这是你真正想要做的。

As dreamlax points out, if you want it to support multiple date formats, you're going to have to call it with each of those formats until one matches.

As for your mismatch on timezone, the timezone for the formatter defaults to the current time zone of the system. Can you show some code that demonstrates how it's off by an hour? I'm not seeing anything like that.

Of course, this is also a very strange thing to be doing. You do know that "SAT 8AM" means "8a local time on the first Saturday after the Reference date," right? So this would be on Jan 3, 1970. It doesn't mean "the next Saturday after today" or anything like that. I can imagine some strange cases where this would be useful, but I wanted to make sure it's what you really meant to do.

或十年 2024-10-23 16:58:17

我不知道为什么它将时间调慢一小时,但要获得晚上 8:30,您需要使用 hh:mma。为了处理 8:30PM 和 8PM 格式,我会设置一个 if else 条件。

I don't know why it would set the time back an hour, but to get 8:30PM you'll need to use hh:mma. And to handle both 8:30PM and 8PM formats, I would set up an if else condition.

左秋 2024-10-23 16:58:17

如果您不确定将收到哪种输入(例如周六上午 8 点或周一上午 8:30),我建议创建一个返回 NSDate 并接受 NSString 作为输入的方法。在此方法中,您将:

  • 使用类似 NSRange range = [incomingString rangeOfString:@":"]; 的内容
  • 如果范围 == nil,则 [formatter setDateFormat:@"eee ha"];
  • else [格式化程序 setDateFormat:@"eee hh:mma"];
  • 最后返回 [formatter dateFromString:dateString];

If you are not sure what kind of input you will receive (ex SAT 8AM or MON 8:30AM), I would suggest creating a method that returns an NSDate and takes an NSString as an input. In this method, you would:

  • use something like NSRange range = [incomingString rangeOfString:@":"];
  • if range == nil, then [formatter setDateFormat:@"eee ha"];
  • else [formatter setDateFormat:@"eee hh:mma"];
  • finally return [formatter dateFromString:dateString];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文