NSDateFormatter给出不同的输出/错误(GMT?)时间

时间:2011-09-17 20:31:03

标签: nsdate nsdateformatter gmt

我已经尝试设置NSDateFormatter的时区和区域设置,但我似乎无法使用任何东西。这是代码

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
NSString *myFireDateString=[@"9/17/11" stringByAppendingString:@" 09:00:00"];
NSDate *myFireDate = [dateFormat dateFromString:myFireDateString];  
NSLog(@"The datestring is is %@",myFireDateString);
NSLog(@"The formatted date is %@",myFireDate);

这是输出:

日期字符串是9/17/11 09:00:00

格式化日期为2011-09-17 13:00:00

1 个答案:

答案 0 :(得分:0)

您需要为dateFormatter设置时区:

[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];

请参阅NSDate from NSString , shows the previous day