简体   繁体   English

NSString到NSDate的转换问题

[英]NSString to NSDate conversion problem

I am having a NSString object which I am converting to NSDate with help of NSDateFormatter. 我有一个NSString对象,我在NSDateFormatter的帮助下转换为NSDate。 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? 输出结束------- 2010-09-24 17:30:00 - 0700客户端输出结束---- 2010-09-25 00:30:00 GMT任何人都可以建议问题出在哪里?

Thanks Pratik Goswami 感谢Pratik Goswami

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. 如果未明确设置格式化程序的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. 这样可以确保输出始终是GMT。

You need to manually set the locale for the NSDateFormatter , so that all your users see the same formatted string. 您需要手动设置NSDateFormatter的语言环境,以便所有用户都能看到相同的格式化字符串。 Otherwise, the formatter will use whatever locale is set for the device. 否则,格式化程序将使用为设备设置的任何区域设置。

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) (下午5:30 + 7小时= 12:30 am)

What's the problem here? 这有什么问题?

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

in iOS 4.x it returns this: 2010-09-25 00:30:00 GMT 在iOS 4.x中,它返回:2010-09-25 00:30:00 GMT

You could submit a bug to apple. 你可以向苹果提交一个bug。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM