Ios 使用NSDateformatter的奇怪行为
首先,我知道这个问题已经被问了几百次了,我大概读了99个问题和答案。 我的问题是我在应用程序中使用时间,我有一个设置,允许用户覆盖实际的系统时间格式。意味着如果系统使用24小时格式,用户仍然可以使用该覆盖设置以12小时格式显示时间 但是,它并不总是有效的,以下是一些情况:Ios 使用NSDateformatter的奇怪行为,ios,objective-c,nsdateformatter,Ios,Objective C,Nsdateformatter,首先,我知道这个问题已经被问了几百次了,我大概读了99个问题和答案。 我的问题是我在应用程序中使用时间,我有一个设置,允许用户覆盖实际的系统时间格式。意味着如果系统使用24小时格式,用户仍然可以使用该覆盖设置以12小时格式显示时间 但是,它并不总是有效的,以下是一些情况: 1) System = 12h Override off: displays 12h format Override on (to 24h): displays 24h format Override on (to 12h)
1) System = 12h
Override off: displays 12h format
Override on (to 24h): displays 24h format
Override on (to 12h): displays 12h format
2) System = 24h
Override off: displays 24h format
Override on (to 24h): displays 24h format
Override on (to 12h): displays 24h format
似乎在案例2)中,我无法让它显示12h格式:-/
我已经试过各种各样的东西了。我从核心数据中获取设置(bool)和时间。这里是我在if语句中设置格式化程序的地方,我使用NSLog对其进行了验证
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if (use24hFormat) {
// we have to use 24h time format
[formatter setDateFormat:@"HH:mm"];
NSLog(@"We have to use 24h-Format);
} else if (!use24hFormat) {
// we have to use 12h time format
[formatter setDateFormat:@"hh:mm a"];
NSLog(@"We have to use 12h-Format);
}
有什么想法吗??谢谢各位
输出:
I believe you, I just ran your code (on iPhone with 24h format not simulator) and it does not work :-( Here is the OUTPUT
2014-06-27 21:02:39.565 Test[687:60b] We have to use 24h-Format
2014-06-27 21:02:39.572 Test[687:60b] timeString: 21:02
2014-06-27 21:02:39.574 Test[687:60b] We have to use 12h-Format
2014-06-27 21:02:39.576 Test[687:60b] timeString: 21:02
Then I ran it on simulator (also 24h format)
[3440:60b] We have to use 24h-Format
[3440:60b] timeString: 21:05
[3440:60b] We have to use 12h-Format
[3440:60b] timeString: 9:05 PM
编辑以提供使用H和H的示例: 有关日期格式代码,请参见: 以下是我的测试,它作为@jcaron也被验证:
NSDate *date = [NSDate date]; // Where my test date is in the PM
BOOL use24hFormat;
NSString *timeString;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
use24hFormat = YES;
if (use24hFormat) {
[formatter setDateFormat:@"H:mm"];
NSLog(@"We have to use 24h-Format");
} else {
[formatter setDateFormat:@"h:mm a"];
NSLog(@"We have to use 12h-Format");
}
timeString = [formatter stringFromDate:date];
NSLog(@"timeString: %@", timeString);
use24hFormat = NO;
if (use24hFormat) {
[formatter setDateFormat:@"H:mm"];
NSLog(@"We have to use 24h-Format");
} else {
[formatter setDateFormat:@"h:mm a"];
NSLog(@"We have to use 12h-Format");
}
timeString = [formatter stringFromDate:date];
NSLog(@"timeString: %@", timeString);
NSLog输出:
我们必须使用24小时格式时间串:21:51
我们必须使用12h格式
时间串:晚上9:51
如果这对OP不起作用,还有其他变量。你读过吗?我已经在12小时格式中使用小写字母h了…@jakepeterson是的,我看到了表格,之前看到过,谢谢你指出它。然而,我也尝试了大写的K,结果也是一样的。对我来说奇怪的是,我只有当系统是24小时,覆盖设置为12小时,而不是相反的方式时才会出现问题。。。它是否可能与日期如何存储在核心数据中有关?我很困惑。当然,
else if
中的条件是多余的。2.刚刚在模拟器中尝试过,无论系统设置如何,我都可以得到我想要的格式。你用什么来格式化日期?您的代码中没有显示这些内容。@Zaph:对不起,我看不出您的评论对我的问题有什么帮助。由于您似乎对我的问题没有解决方案,我恳请您仅以有用和各自的方式作出贡献/回应。我洗耳恭听你是否有任何错误报告。顺便说一句,我使用的是Xcode 5(iOS SDK 7.1),我不太确定世界上哪里有人从1数到24或从0数到11的小时数。据我所知,大多数人使用0到23(24小时制,从0:00到23:59)或1到12(12小时制,从上午12:00到上午11:59,然后是下午12:00到晚上11:59)。所以应该是h和h,而不是k和k。谢谢Zaph,我以前试过使用kk和kk,但都不起作用。我相信你,我只是运行了你的代码(在24小时格式的iPhone上,而不是模拟器上),但它不工作:-(见输出的更新问题这太奇怪了,当我在我的iPhone上运行它时(7.1使用24小时格式),如果我在mac上的模拟器上运行它,它就不工作了(osx 10.9使用24小时格式)我在我的iPhone 5s(iOS 7.1.1)上运行了它,得到了正确的结果。对你来说,它不起作用,我明白。你在一个24小时工作的地区吗?你的时区是什么?