NSdate Assuming wrong time zone

限于喜欢 提交于 2019-11-26 14:58:36

问题


I am trying to convert the following string into an NSDate object:

NSString *str=@"25 May 2012 10:25:00";

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"d MMM yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"asia/kolkata"]];
 NSDate *date = [dateFormatter dateFromString:str];


In console : date-->2012-05-25 04:55:00 +0000....it lags behind 5 hours and 30 minutes and assumes GMT timezone instead of Asia...Why it is so?

回答1:


When you see an [NSDate description] printed in the console, it is always the corresponding time in GMT. If you use the same date formatter to convert the date back to a string, it should be in the specified time zone.

An [NSDate description] is what you see if you type

po date

or

po [date description]

or you use NSLog to send either one of these forms to the console.




回答2:


if you are looking for India Timezone you should use:



来源:https://stackoverflow.com/questions/9442126/nsdate-assuming-wrong-time-zone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!