NSdate假设时区错误 [英] NSdate Assuming wrong time zone

查看:95
本文介绍了NSdate假设时区错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以下字符串转换为NSDate对象:

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?


推荐答案

当您看到[NSDate说明]打印在控制台,它始终是GMT的相应时间。如果您使用相同的日期格式化程序将日期转换回字符串,它应该在指定的时区。

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.

您看到的是[NSDate说明]你输入

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

po date

po [date description]

或者您使用NSLog将其中一个表单发送到控制台。

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

这篇关于NSdate假设时区错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆