NSString到NSDate的转换问题 [英] NSString to NSDate conversion problem
问题描述
我有一个NSString对象,我在NSDateFormatter的帮助下转换为NSDate。现在代码在这里适用于所有操作系统,但它在客户端的添加(美国地区)创建不同的输出。
这是我正在使用的代码。
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);
我的结束输出------- 2010-09-24 17:30:00 - 0700
客户端输出---- 2010-09-25 00:30:00 GMT
任何人都可以建议问题出在哪里?
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?
谢谢
Pratik Goswami
Thanks Pratik Goswami
推荐答案
我注意到的唯一输出差异是时间不同。如果您没有明确设置格式化程序的 timeZone
属性,它将使用系统的本地时区。如果您希望时间与您和您的客户完全相同:
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]];
这样可确保输出始终为GMT。
That would insure the output is always GMT.
这篇关于NSString到NSDate的转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!