iphone:NSDateFormatter 返回错误的格式

发布于 2024-12-29 14:11:45 字数 351 浏览 2 评论 0原文

我添加了这段代码

NSString *dateString=@"March 29, 2012 12:45 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM dd, yyyy h:mm aaa"];
NSDate *date = [formatter dateFromString:dateString];

,但我收到以下输出:

2012-03-29 07:15:00 +0000

我怎样才能获得实际的格式?

I added this code

NSString *dateString=@"March 29, 2012 12:45 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM dd, yyyy h:mm aaa"];
NSDate *date = [formatter dateFromString:dateString];

But i am receiving the following output:

2012-03-29 07:15:00 +0000

How can i get the actual format?

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

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

发布评论

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

评论(1

记忆消瘦 2025-01-05 14:11:45

时间显示不正确的原因是,当您记录 NSDate 时,它​​会以您当地的时区向您显示时间。

如果要将日期格式化程序设置为特定时区,可以使用 setTimeZone: 方法。我注意到您的个人资料显示您位于印度,如果我没记错的话,印度使用半小时时区,这将占其报告的 7:15 时间,而不是 12:45。

The reason that the time is displaying incorrectly is because when you log the NSDate it will show it to you in your local time zone.

If you want to set your date formatter to a specific time zone you can use the setTimeZone: method. I noticed that your profile says you are located in India, and if I recall correctly, India utilizes half hour time zones that would account for the 7:15 time that it is reporting instead of 12:45.

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