是否可以解析“Fri, 24 Feb 2012 20:00:00 GMT”使用 NSDateFormatter 吗?

发布于 2025-01-08 14:07:27 字数 110 浏览 5 评论 0原文

我正在尝试解析

 Fri, 24 Feb 2012 20:00:00 GMT

是否可以使用 NSDateFormatter 来做到这一点?

I'm trying to parse

 Fri, 24 Feb 2012 20:00:00 GMT

Is it possible to do this with using NSDateFormatter?

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

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

发布评论

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

评论(2

高速公鹿 2025-01-15 14:07:27

请尝试以下操作:

NSString *dateString = @"Fri, 24 Feb 2012 20:00:00 GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"EEE, d MMM yyyy HH:mm:ss zzz";

NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"%@", date);

根据您指定时区的方式,您可能需要更改该行:

dateFormatter.dateFormat = @"EEE, d MMM yyyy HH:mm:ss zzz";

例如

dateFormatter.dateFormat = @"EEE, d MMM yyyy HH:mm:ss Z";

,如果您将使用 GMT-02:00,则保留它,但如果您将使用 PDT,则保留它。

Try the following:

NSString *dateString = @"Fri, 24 Feb 2012 20:00:00 GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"EEE, d MMM yyyy HH:mm:ss zzz";

NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"%@", date);

Depending on how you will specify the time zone, you may have to change the line:

dateFormatter.dateFormat = @"EEE, d MMM yyyy HH:mm:ss zzz";

with

dateFormatter.dateFormat = @"EEE, d MMM yyyy HH:mm:ss Z";

if you will use GMT-02:00 for example, but keep it if you will use PDT.

小红帽 2025-01-15 14:07:27

当然你应该使用面具..

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] ];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSDate *date = [dateFormatter dateFromString:dateString];

它可以工作。

Sure you should use a mask..

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] ];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSDate *date = [dateFormatter dateFromString:dateString];

It could work.

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