我正在使用 NSDateFormatter,它给了我奇怪的结果
NSString *startDateStr = @"2014-05-17 11:00 AM";
NSString *endDateStr = @"2014-05-17 11:30 AM"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm a"];
startDate = [formatter dateFromString:startDateStr];
endDate = [formatter dateFromString:endDateStr];
不管我用不
[formatter setTimeZone:[NSTimeZone localTimeZone]];
或者不是..它给了我一个结果
2014-05-16 21:00:00 +0000
对于开始日期 和
2014-05-16 21:30:00 +0000 For endDate
我不知道这可能是什么问题!
最佳答案
我想你住在澳大利亚附近的某个地方?
您需要了解 NSDate 的作用和显示。 NSDate 显示 GMT 时间。因此,如果您将上午 11 点转换为您本地的时区,同时在格林尼治标准时间(即伦敦附近的格林威治,当时他们没有夏令时)是晚上 9 点,那么 NSDate 将显示晚上 9 点。你绝对有正确的日期和时间,你只需要习惯 NSDate 显示它的方式。
如果纽约的人运行您的代码,他们会得到不同的结果,因为纽约的“本地时间上午 11 点”与您居住地的“本地时间上午 11 点”不同。
设置 [NSTimeZone localTimeZone] 没有什么区别,因为这是日期格式化程序的默认值。
关于iOS:NSDateFormatter 自动更改时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710532/