Dateformatter将时区应用于日期时间

时间:2011-01-03 20:00:53

标签: iphone cocoa-touch nsdateformatter

我的日期时间字段因时区而变得有问题。

传入的日期时间是 - 2010-12-28 19:10:00

只有当我使用从字符串中获取日期时才会显示为 - 2010-12-29 00:10:00 +0000

我在做

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[[dateFormatter locale] localeIdentifier];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

p.date = [dateFormatter dateFromString:[values objectAtIndex:6]];

就像它适用GMT一样,我的日期时间增加了5个小时。我怎样才能阻止这种情况发生。

感谢 谢丽尔

1 个答案:

答案 0 :(得分:-1)

假设您当地的时间是GMT-5,那就完全没问题。 2010-12-29 00:10:00 +0000和2010-12-28 19:10:00 -0500两者都确定了完全相同的时间点。这就是NSDate代表的全部内容:单个时间点。显示它的时区是在创建另一个日期格式化程序时将日期转换回字符串以供显示。