Dateformatter iPhone问题

时间:2011-06-08 07:10:23

标签: iphone nsdate nsdateformatter

NSLog(@"%@", self.departDate);

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSLog(@"%@", [dateFormat stringFromDate:self.departDate]);
[dateFormat release];

所以我有这个代码,它在控制台中返回:

2011-06-09 00:00:00 +0000
06/08/2011

为什么会发生这种情况?如您所见,我的self.departDate对象返回2011-06-09作为它的日期。当我尝试使用日期格式化程序进行转换时,我失去了一天的WTF?

1 个答案:

答案 0 :(得分:0)

第一个日志对应GMT日期。但是,当您创建NSDateFormatter实例时,它会将默认时区设置为用户的时区,我猜测它可能位于格林威治以西的某个地方。如果您希望时间保留在GMT中,则必须添加此行,

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];