NSString 到 NSDate 转换问题

发布于 2024-09-19 11:28:12 字数 509 浏览 3 评论 0原文

我有一个 NSString 对象,我正在 NSDateFormatter 的帮助下将其转换为 NSDate。现在,代码在所有操作系统上都可以正常工作,但它在客户端添加(美国地区)时创建不同的输出。 这是我正在使用的代码。

NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strConcat = @"2010-09-24 17:30:00";
NSDate *date = [formate dateFromString:strConcat];
NSLog(@"Output :%@",date);

我最后的输出--------2010-09-24 17:30:00 - 0700 客户端输出 ----2010-09-25 00:30:00 GMT 任何人都可以建议问题出在哪里吗?

谢谢 普拉蒂克·戈斯瓦米

I am having a NSString object which I am converting to NSDate with help of NSDateFormatter. Now code works fine here with all the OS but it is creating different Output at client's add (USA region).
Here is the code that I am using.

NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strConcat = @"2010-09-24 17:30:00";
NSDate *date = [formate dateFromString:strConcat];
NSLog(@"Output :%@",date);

Output at my end -------2010-09-24 17:30:00 - 0700
Output at Client end ----2010-09-25 00:30:00 GMT
Can anyone please suggest where's the problem?

Thanks
Pratik Goswami

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

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

发布评论

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

评论(4

累赘 2024-09-26 11:28:12

我注意到的唯一输出差异是时间不同。如果您没有显式设置格式化程序的 timeZone 属性,它将使用系统的本地时区。如果您希望您和您的客户的时间完全相同:

[formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

这将确保输出始终为 GMT。

The only output difference I notice is the time is different. If you do not explicitly set the timeZone property of the formatter it will use the system's local timezone. If you expect the times to the be the exact same from you and your client:

[formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

That would insure the output is always GMT.

禾厶谷欠 2024-09-26 11:28:12

您需要手动设置 NSDateFormatter 的区域设置,以便所有用户看到相同的格式化字符串。否则,格式化程序将使用为设备设置的任何区域设置。

You need to manually set the locale for the NSDateFormatter, so that all your users see the same formatted string. Otherwise, the formatter will use whatever locale is set for the device.

明月夜 2024-09-26 11:28:12

事实上,它工作正常。这两个日期是等效的,只是一个日期位于美国/山地时区,另一个日期位于格林威治时区。 (5:30pm + 7 小时 = 12:30am)

这里有什么问题?

Actually, it's working correctly. The two dates are equivalent, just that one is in the US/Mountain time zone and the other is in the Greenwich time zone. (5:30pm + 7 hours = 12:30 am)

What's the problem here?

倚栏听风 2024-09-26 11:28:12

在 iOS2.x 和 3.x 中,description/datefromstring 函数返回:
2010-09-24 17:30:00 - 0700

在 iOS 4.x 中它返回:
2010-09-25 00:30:00 GMT

您可以向苹果提交错误。

In iOS2.x and 3.x the description/datefromstring function returns:
2010-09-24 17:30:00 - 0700

in iOS 4.x it returns this:
2010-09-25 00:30:00 GMT

You could submit a bug to apple.

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