objective-c NSDateFormatter 24 小时制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2135267/
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
NSDateFormatter with 24 hour times
提问by John
I have a countdown timer which countsdown from the current date/time to a specific future date/time. It is working great except for one problem. I input the future date using NSDateFormatterand dateFromString. It doesn't seem to be able to accept any time (hour) over 12 though indicating it is not support 24 hour clock. Is there a way to enable 24 hour clock support or a workaround? Here is some of my code:
我有一个倒数计时器,它从当前日期/时间倒计时到特定的未来日期/时间。除了一个问题外,它工作得很好。我使用NSDateFormatter和输入未来日期dateFromString。尽管表明它不支持 24 小时制,但它似乎无法接受超过 12 小时的任何时间(小时)。有没有办法启用 24 小时时钟支持或解决方法?这是我的一些代码:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString:@"2010-03-14 15:00:00"];
回答by VoidPointer
NSDateFormatter follows the Unicode standard for date and time patterns. Use 'H' for the hour on a 24-hour clock:
NSDateFormatter 遵循日期和时间模式的Unicode 标准。使用 'H' 表示 24 小时制的小时:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myDate = [df dateFromString:@"2010-03-14 15:00:00"];
回答by crojassoto
I had the same problem and using HH worked only on some devices, like Roger also verified. In the end this was the solution that worked for me, I hope it works for others. Finding this answer was difficult, there are no forums with it, it was literally trial and error following the apple documentation.
我遇到了同样的问题,使用 HH 只能在某些设备上工作,如 Roger 也验证过。最后,这是对我有用的解决方案,我希望它对其他人有用。找到这个答案很困难,没有论坛,它实际上是按照苹果文档进行的反复试验。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
NSString *dateFormat = @"dd/MM/yyyy HH:mm"; //MM for month, mm for minutes
[dateFormatter setDateFormat:dateFormat];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
date = [dateFormatter dateFromString:string];
回答by EvGeniy Ilyin
My solution on Swift:
我在 Swift 上的解决方案:
let formatter = NSDateFormatter()
var defIdentifer = formatter.locale.localeIdentifier
if !defIdentifer.hasSuffix("_POSIX") {
defIdentifer = defIdentifer+"_POSIX"
let locale = NSLocale(localeIdentifier: defIdentifer)
formatter.locale = locale
}
formatter.dateFormat = "HH:mm"
回答by Harry Bloom
Taken from the Apple Technical Q&Aon NSDateFormatters
摘自Apple Technical Q&Aon NSDateFormatters
Q: I'm using NSDateFormatter to parse an Internet-style date, but this fails for some users in some regions. I've set a specific date format string; shouldn't that force NSDateFormatter to work independently of the user's region settings?
A: No. While setting a date format string will appear to work for most users, it's not the right solution to this problem. There are many places where format strings behave in unexpected ways.
问:我正在使用 NSDateFormatter 来解析 Internet 样式的日期,但是对于某些地区的某些用户来说这会失败。我已经设置了一个特定的日期格式字符串;这不应该强制 NSDateFormatter 独立于用户的区域设置工作吗?
A: 不可以。虽然设置日期格式字符串似乎对大多数用户都有效,但这并不是解决此问题的正确方法。有很多地方格式字符串的行为方式出乎意料。
This is how I have done mine in Swift:
这就是我在 Swift 中的做法:
private let dateFormatter: NSDateFormatter = {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone.init(forSecondsFromGMT: 0)
return dateFormatter
}()
回答by Chris
I had a similar problem recently, instead of HH, NSDateFormatterignored hh, a(AM/PM Symbol) and G(cyclic era name) in my app.
我最近遇到了类似的问题,而不是HH,在我的应用程序中NSDateFormatter忽略了hh,a(AM/PM 符号)和G(循环时代名称)。
And I was surprised to find that if I go to localization setting of my device and make some random choice, all the freaks are gone and the error cannot be produced again. Very weird.
我惊讶地发现,如果我去我的设备的本地化设置并随机选择,所有的怪胎都消失了,错误不会再次产生。很奇怪。
Then I tested on simulator to do some study on it. There is my solution:
然后我在模拟器上进行了测试以对其进行一些研究。有我的解决方案:
After you created the NSDateFormatter, explicitly set the locale property even you are using current locale, more importantly, DON'T use [NSLocale currentLocale], this one is bugged and can be somehow "overriden" by user setting, use systemLocaleor explicitly create an NSLocaleinstance using a locale identifer.
创建 后NSDateFormatter,即使您正在使用当前语言环境,也显式设置语言环境属性,更重要的是,不要使用[NSLocale currentLocale],这个有问题并且可以通过用户设置以某种方式“覆盖”,使用systemLocale或显式创建NSLocale使用语言环境标识符的实例.
回答by Denis Kutlubaev
Objective C version of getting NSDate from 24-hour string when user has set 12 hour format on their iPhone without changing locale and setting timezone:
当用户在其 iPhone 上设置 12 小时格式而不更改区域设置和设置时区时,从 24 小时字符串获取 NSDate 的目标 C 版本:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *localeId = dateFormatter.locale.localeIdentifier;
if (! [localeId hasSuffix:@"_POSIX"]) {
localeId = [localeId stringByAppendingString:@"_POSIX"];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:localeId];
}
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH.mm.ss";
NSDate *date = [dateFormatter dateFromString:dateText];

