Behavior of NSDateFormatter in iOS 5.1 and iOS 6

…衆ロ難τιáo~ 提交于 2019-12-13 01:34:15

问题


Anyone else noticing some differences between iOS 5.1 and iOS 6.0? In the following, the format of the date string comes back differently in each version:NSDateFormatter

*dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss (vvvvv)";
NSDate   *lDate   = [[NSDate alloc]init];
NSString *sDate   = [dateFormatter stringFromDate:lDate];

In iOS 5.1, sDate is "10/01/2012 16:43:09 (GMT-05:00)" but in iOS 6.0, sDate is "10/01/2012 16:44:09 ()"

Any ideas on why this would be?


回答1:


Replace @"MM/dd/yyyy HH:mm:ss (vvvvv)"; with @"MM/dd/yyyy HH:mm:ss ('GMT'Z)";

The Z format string will output what you were doing with vvvvv except for the GMT prefix and there is no colon as well.

If the colon is needed, you can add this line to add it to the string:

sDate = [NSString stringWithFormat:@"%@:%@", [sDate substringToIndex:sDate.length-3], [sDate substringWithRange:NSMakeRange(sDate.length-3, 3)]];



回答2:


I think that this solve your problem (only use one v):

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss (V)";
NSDate   *lDate   = [[NSDate alloc]init];
NSString *sDate   = [dateFormatter stringFromDate:lDate];



回答3:


I have IST instead of GMT. I used V for that in IOS 5 working fine but in IOS 6 not working.



来源:https://stackoverflow.com/questions/12681577/behavior-of-nsdateformatter-in-ios-5-1-and-ios-6

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