从字符串错误地格式化 NSDate

发布于 2024-12-14 11:58:06 字数 551 浏览 1 评论 0原文

我想从字符串创建 NSDate:

NSString *mostRecentMentionMessageTimestamp = [mostRecentMessage valueForKey:@"updated_at"];
NSLog(@"%@",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(@"%@",mostRecentMentionDate);

当我打印字符串时,它是: 2011-11-10T07:22:59Z

在我从字符串创建日期并打印它后,它是 (null)

我做错了什么?

I would like to create an NSDate from a string:

NSString *mostRecentMentionMessageTimestamp = [mostRecentMessage valueForKey:@"updated_at"];
NSLog(@"%@",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(@"%@",mostRecentMentionDate);

When I print my string it is:
2011-11-10T07:22:59Z

After I make a date from string and print it, it is (null)

What am I doing wrong?

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

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

发布评论

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

评论(3

当梦初醒 2024-12-21 11:58:06

您需要设置时区并在日期格式末尾删除 Z。由于您的日期有一个 Z,因此它采用 UTC。所以你可以使用:

[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"UTC"]];
[dateFormatter setDateFormat: @"yyyy-MM-dd'T'HH:mm:ss"];

You need to set the time zone and drop the Z at the end of the date format. Since your date has a Z, it is in UTC. So you can use:

[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"UTC"]];
[dateFormatter setDateFormat: @"yyyy-MM-dd'T'HH:mm:ss"];
情痴 2024-12-21 11:58:06

格式几乎正确,使用:

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];

解决时区问题使用@Jef add的解决方案:

[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"UTC"]];

完整示例代码:

NSString *mostRecentMentionMessageTimestamp = @"2011-11-10T07:22:59Z";
NSLog(@"input:  %@",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(@"output: %@",mostRecentMentionDate);

NSLog输出:

input:  2011-11-10T07:22:59Z
output: 2011-11-10 07:22:59 +0000

The format is almost correct, use:

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];

to solve the time zone issue use the solution by @Jef add:

[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"UTC"]];

Full example code:

NSString *mostRecentMentionMessageTimestamp = @"2011-11-10T07:22:59Z";
NSLog(@"input:  %@",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: @"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(@"output: %@",mostRecentMentionDate);

NSLog output:

input:  2011-11-10T07:22:59Z
output: 2011-11-10 07:22:59 +0000
谜泪 2024-12-21 11:58:06

您尝试转换为 NSDate 的字符串值不正确。
“2011-11-10T07:22:59Z”
它应该有一个时区差异值来代替“Z”,以使其与日期格式化程序的格式兼容。

例如 2011-11-10T07:22:59+0430

the string value which you are trying to convert to NSDate is incorrect.
"2011-11-10T07:22:59Z"
it should have a timezone difference value in place of "Z" to make it compatible to date formatter's format.

e.g. 2011-11-10T07:22:59+0430

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