xcode 为什么我的 NSDateFormatter 有时会以 yyyyMMddHHmmssSSS 返回上午或下午?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7072267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:44:28  来源:igfitidea点击:

Why does my NSDateFormatter sometimes return an a.m. or p.m. with yyyyMMddHHmmssSSS?

iphoneobjective-cxcodecocoa-touchios4

提问by Ethan Allen

Sometimes my code is returning an a.m. or p.m. but not always. Most of the time it just returns what I expect, which is something like: 20110815170852164

有时我的代码返回 am 或 pm 但并非总是如此。大多数时候它只返回我期望的内容,类似于:20110815170852164

But other times it's returning: 20100412010241 a.m.450

但其他时候它会返回:20100412010241 am450

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyyMMddHHmmssSSS"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];

What could be causing this? Specific date/time country settings on users iPhones? I have this out to thousands of people, and most aren't returning the a.m. (which is expected) but others are. WHY?

什么可能导致这种情况?用户 iPhone 上的特定日期/时间国家/地区设置?我向成千上万的人传达了这个信息,大多数人没有返回上午(这是预期的),但其他人是。为什么?

回答by mopsled

The problem you are describing is a known bug. Check out some discussion on the problem on stackoverflow, and you can find some possible work-arounds there.

您所描述的问题是一个已知错误。查看有关 stackoverflow 问题的一些讨论,您可以在那里找到一些可能的解决方法。

Here's an excerpt of huyz's explaination of the bug:

以下是 huyz 对该错误的解释的摘录:

The problem comes from NSDateFormatter somehow “getting stuck” in the 12 or 24-hour time mode that the user has manually selected. So if a French user manually selects 12-hour mode, and the application requested NSDateFormatter to output time with the 24-hour format “HHmm”, it would actually receive time in a 12-hour format, e.g. “01:00 PM”, as if the application had instead requested “hhmm aa”. The reverse would happen if a US user manually selected 24-hour mode: outputting time with the 12-hour format “hhmm aa” would actually get you time in the 24-hour format instead, e.g. “17:00″.

问题来自 NSDateFormatter 不知何故“卡在”用户手动选择的 12 或 24 小时时间模式中。因此,如果法国用户手动选择 12 小时模式,并且应用程序请求 NSDateFormatter 以 24 小时格式输出时间“HHmm”,它实际上会以 12 小时格式接收时间,例如“01:00 PM”,好像应用程序反而请求了“hhmm aa”。如果美国用户手动选择 24 小时模式,则会发生相反的情况:以 12 小时格式输出时间“hhmm aa”实际上会让您以 24 小时格式获得时间,例如“17:00”。

回答by Hot Licks

Right. The solution is to explicitly set the locale of the date formatter, ideally to en_US_POSIX. See the final answer to thisquestion.

对。解决方案是显式设置日期格式化程序的区域设置,最好设置为 en_US_POSIX。请参阅问题的最终答案。

回答by Barnabas Kecskes

I was working on a data-share function of a project which was highly depending on the values/results entered in advance when I bumped into the same problem. I implemented a nasty little workaround for saving the right datetime in 24h format to the database.

我正在研究一个项目的数据共享功能,当我遇到同样的问题时,该功能高度依赖于预先输入的值/结果。我实现了一个讨厌的小变通方法,用于将正确的日期时间以 24 小时格式保存到数据库中。

The solution is very simple

解决方法很简单

First, I get the local date from the device:

首先,我从设备获取本地日期:

NSDate* now = [NSDate date];

Second step is to create a NSDateFormatterwith US locale regardless of system's locale to make sure that "PM" is going to be "PM":

第二步是NSDateFormatter不管系统的语言环境如何创建一个美国语言环境,以确保“PM”将是“PM”:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];

Then, I get the date (year, month, day) into a string:

然后,我将日期(年、月、日)放入一个字符串中:

[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *date = [formatter stringFromDate:now];

For the next part, I get the hours, minutes and seconds:

对于下一部分,我得到小时、分钟和秒:

[formatter setDateFormat:@"hh"];
NSString *hours = [formatter stringFromDate:now];
[formatter setDateFormat:@"mm"];
NSString *minutes = [formatter stringFromDate:now];
[formatter setDateFormat:@"ss"];
NSString *seconds = [formatter stringFromDate:now];

Next, I get the part of the day:

接下来,我得到了当天的部分:

[formatter setDateFormat:@"a"];
NSString *partOfDay = [formatter stringFromDate:now];

Then comes the logic: if the part of the day is PM but the hours are less then 12, we just have to correct that. In code:

然后是逻辑:如果一天中的部分时间是下午但小时数少于 12,我们只需要更正它。在代码中:

if([partOfDay isEqualToString:@"PM"] && [hours intValue] < 12) {
    hours = [[NSString alloc] initWithFormat:@"%i", ([hours intValue] + 12)];
}

After this modification, we are ready to put the string together in order to have a datetime ready to be saved into SQL:

在此修改之后,我们准备将字符串放在一起,以便准备好将日期时间保存到 SQL 中:

NSString *sqlDateTime = [[NSString alloc] initWithFormat:@"%@ %@:%@:%@", date, hours, minutes, seconds];

That's all there is to it. And from now we can only hope that Apple is going to fix this bug to make it work as it should be.

这里的所有都是它的。从现在开始,我们只能希望 Apple 能够修复这个错误,使其正常工作。

I hope this helps.

我希望这有帮助。