【问题标题】:iOS 4.2 DateFormatteriOS 4.2 日期格式化程序
【发布时间】:2011-02-06 14:56:07
【问题描述】:

以下代码在 iOS 4.2 之前的应用中运行。

NSString *sunday = @"2011-03-13 20:15 -04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm Z"];
NSDate *myDate = [dateFormatter dateFromString:sunday];

NSLog(@"sunday: %@ myDate: %@ fromNow: %d", sunday, myDate, [myDate timeIntervalSinceNow]);

输出:

sunday: 2011-03-13 20:15 -04:00 myDate: (null) fromNow: 0

我在这里做错了什么? myDate 是 null。 dateFormmater 有什么东西发生了变化,我失踪了。我想这在这一点上是微不足道的,因为我一直在盯着这个......

【问题讨论】:

    标签: iphone ios-4.2


    【解决方案1】:

    如果我正确理解问题,Z 说明符会同时解析“-0400”和“GMT-04:00”,而不是“-04:00”。 UTS #35 似乎证实了这一点。不过,在我的测试中,它甚至解析了“GMT-0400”。所以“-04:00”看起来确实是 NSDateFormatter 拒绝理解的唯一时区格式。

    我可以愤世嫉俗地建议Apple's bug reporter 作为答案吗?

    【讨论】:

    • NSDateFormatter 以前理解它,iOS
    【解决方案2】:

    尝试引用你的破折号,例如:

      @"yyyy'-'MM'-'dd HH':'mm Z"
    

    我在这里的最佳阅读:http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns 似乎表明这实际上是必需的,尽管我似乎记得过去在没有问题的 iOS 上使用过相同的日期格式模式。

    【讨论】:

    • 没用。从其他帖子来看,这似乎是 iOS > 4 的问题。
    猜你喜欢
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2015-06-17
    • 2017-04-29
    相关资源
    最近更新 更多