NSDateFormatter gives different output/wrong (GMT?) time

和自甴很熟 提交于 2019-12-11 07:39:59

问题


I have tried setting the timezone and locale of the NSDateFormatter but I can't seem to get anything to work. Here is the code

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);

Here is the output:

The datestring is is 9/17/11 09:00:00

The formatted date is 2011-09-17 13:00:00


回答1:


You need to set a timezone to your dateFormatter:

[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];

See NSDate from NSString , shows the previous day



来源:https://stackoverflow.com/questions/7457590/nsdateformatter-gives-different-output-wrong-gmt-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!