iOS 5.1 和 iOS 6 中 NSDateFormatter 的行为 [英] Behavior of NSDateFormatter in iOS 5.1 and iOS 6
问题描述
还有人注意到 iOS 5.1 和 iOS 6.0 之间的一些差异吗?在下面,日期字符串的格式在每个版本中都不同:NSDateFormatter
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];
在 iOS 5.1 中,sDate 是10/01/2012 16:43:09 (GMT-05:00)"但在 iOS 6.0 中,sDate 是10/01/2012 16:44:09 ()"
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?
推荐答案
将 @"MM/dd/yyyy HH:mm:ss (vvvvv)";
替换为 @"MM/dd/yyyy HH:mm:ss ('GMT'Z)";
Z
格式字符串将输出您对 vvvvv
所做的操作,除了 GMT
前缀并且没有冒号.
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)]];
这篇关于iOS 5.1 和 iOS 6 中 NSDateFormatter 的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!