Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 具有24小时时间的NSDateFormatter_Objective C_Iphone_Nsdateformatter - Fatal编程技术网

Objective c 具有24小时时间的NSDateFormatter

Objective c 具有24小时时间的NSDateFormatter,objective-c,iphone,nsdateformatter,Objective C,Iphone,Nsdateformatter,我有一个倒计时计时器,它从当前日期/时间倒计时到特定的未来日期/时间。除了一个问题外,它工作得很好。我使用NSDateFormatter和dateFromString输入未来日期。虽然表示不支持24小时制,但它似乎无法接受12小时以上的任何时间(小时)。是否有办法实现24小时不间断支持或解决方案?以下是我的一些代码: NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd hh:mm

我有一个倒计时计时器,它从当前日期/时间倒计时到特定的未来日期/时间。除了一个问题外,它工作得很好。我使用
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"];

NSDateFormatter遵循。在24小时制时钟上使用“H”表示小时:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *myDate = [df dateFromString:@"2010-03-14 15:00:00"];

我也有同样的问题,使用HH只能在一些设备上工作,比如罗杰也验证了这一点。最后,这是对我有效的解决方案,我希望它对其他人有效。找到这个答案是困难的,没有论坛,这是字面上的尝试和错误后,苹果的文档

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];

我最近遇到了一个类似的问题,我的应用程序中没有出现
HH
NSDateFormatter
被忽略
HH
a
(AM/PM符号)和
G
(循环纪元名称)

我惊讶地发现,如果我去我的设备的本地化设置,并作出一些随机选择,所有的怪胎都消失了,错误不能再次产生。很奇怪

然后我在模拟器上进行了测试,对其进行了一些研究。我的解决方案是:

创建
NSDateFormatter
后,即使使用当前区域设置,也要显式设置区域设置属性,更重要的是,不要使用
[NSLocale currentLocale]
,此设置存在错误,可以通过用户设置以某种方式“覆盖”,使用
systemLocale
或使用区域设置标识符显式创建
NSLocale
实例。

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"
取自NSDateFormatters上的

问:我正在使用NSDateFormatter解析一个Internet样式的日期,但对于某些地区的一些用户来说,这是失败的。我已经设置了一个特定的日期格式字符串;这不应该强制NSDateFormatter独立于用户的区域设置工作吗

答:不会。虽然设置日期格式字符串似乎对大多数用户都有效,但这不是解决此问题的正确方法。在许多地方,格式字符串的行为都是出乎意料的

以下是我在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
}()

当用户在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];

令人惊讶,不是吗?我刚刚发现我自己的一个应用程序失败了,因为尽管在格式字符串中使用了HH,但用户设置会覆盖它。疯子这个答案有误导性。我同意Mavrick3和Roger的观点。用户设置覆盖标准时间模式的HH。日期格式化文档,令人恼火地暗示了这种行为:在所有情况下,您应该考虑格式化程序默认使用用户的区域设置(CurrutLoALAL)与用户偏好设置叠加。如果原始格式中的日期格式为24小时,用户的区域设置为12小时,我们应该怎么办?在这种情况下,[[NSDate date]timeIntervalSinceDate:@“我的24小时datetime”]返回“nan”…非常烦人!有人能告诉我如何使这个工作吗。基本上,我需要始终使用24小时时钟来计算TimeIntervalenceDate,而不管用户的区域设置如何。我不知道如何执行此操作。
NSDateFormatter
中解释了其行为。若要覆盖用户的设置,请直接设置日期格式化程序的区域设置:
df.locale=…
如何进行本地化。。。?您是否应该先将
NSDateFormatter
正确本地化?这应该标记为答案。这完全是可行的。这在技术问答QA1480中有解释:天哪,这是可行的,但这是一个棘手的问题,尽管答案很好!这就是我要找的,这就是我要找的。