使用 NSDateFormatter 从字符串中获取日期,无论 12 小时到 24 小时设置如何

发布于 2024-08-31 17:49:00 字数 694 浏览 2 评论 0原文

今天我的问题是关于日期格式和字符串。 我的应用程序从互联网下载一些表示日期的字符串。日期格式始终是这样的:“2010-05-24 at 20:45” 我需要将此字符串转换为 NSDate 对象才能执行一些日期操作。 我尝试了这段代码:

NSString * dateString = @"2010-05-24 at 20:45" // actually downloaded from the internet
NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
NSDate * dateFromString = [myDateFormatter dateFromString:dateString];

只要 iPhone 设置为 24 小时时钟,这似乎就可以完美工作。只要我在常规设置中切换到 12 小时时钟,就不会创建 NSDate 对象(并且应用程序会愉快地崩溃) 我读过其他人也有类似的问题,但我似乎没有找到解决方案。 现在的问题是: 只要我指定它应该使用哪种格式来解析字符串(它是不可修改的,因为它来自网络),NSDateFormatter 对用户设置是敏感的,这对我来说没有意义,有什么用检查用户设置? 更重要的是,如何执行我的任务(从字符串中获取 NSDate 对象,无论用户设置如何)?

Today my question is about date formats and strings.
My application downloads some strings representing dates from the internet. The date format is always like this: "2010-05-24 at 20:45"
I need to convert this string into an NSDate object in order to perform some date manipulations.
I tried this code:

NSString * dateString = @"2010-05-24 at 20:45" // actually downloaded from the internet
NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
NSDate * dateFromString = [myDateFormatter dateFromString:dateString];

This seems to work perfectly fine as long as the iPhone is set to 24h clock. As long as I switch to 12h clock in the general settings the NSDate object is not created (and the application happily crashes)
I have read others having similar problems but I don't seem to find a solution.
Now the questions:
It doesn't make sense to me that the NSDateFormatter is sensible to user settings as long as I am specifying which format it is supposed to use to parse the string (which is not modifiable because it comes from the web), what's the use of checking user settings?
More important, how can I get my task performed (to get an NSDate object from the string regardless the user settings)?

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

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

发布评论

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

评论(3

我做我的改变 2024-09-07 17:49:00

解释了此问题背后的原因以及如何解决它。

This explains the reason behind this problem and how to solve it.

仅一夜美梦 2024-09-07 17:49:00
NSString * dateString = @"2010-05-24 at 20:45" // actually downloaded from the internet
NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd 'at' hh:mm"];
NSDate * dateFromString = [myDateFormatter dateFromString:dateString];

设置日期格式时使用“hh”而不是“HH”

NSString * dateString = @"2010-05-24 at 20:45" // actually downloaded from the internet
NSDateFormatter * myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd 'at' hh:mm"];
NSDate * dateFromString = [myDateFormatter dateFromString:dateString];

use "hh" instead of "HH" in setting date format

霊感 2024-09-07 17:49:00

我发现苹果的这篇文章准确地解释了这个问题! (https://developer.apple.com/library/content/qa /qa1480/_index.html

但无论如何,简短的答案是您需要将区域设置设置为“en_US_POSIX”,以便它可以与任何用户设置的日期相关配置一起使用。

let stringDate = "2017-03-15T11:37:16-03:00" // ISO8601 formatted string

let iso8601Formatter = DateFormatter()
iso8601Formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
iso8601Formatter.locale = Locale(identifier: "en_US_POSIX")
iso8601Formatter.date(from: stringDate)

I've found this article from Apple that explains exactly this issue! (https://developer.apple.com/library/content/qa/qa1480/_index.html)

but anyway, the shortchanged answer is that you need to set the locale to "en_US_POSIX" for it to work with any user-set date related configs.

let stringDate = "2017-03-15T11:37:16-03:00" // ISO8601 formatted string

let iso8601Formatter = DateFormatter()
iso8601Formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
iso8601Formatter.locale = Locale(identifier: "en_US_POSIX")
iso8601Formatter.date(from: stringDate)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文