【问题标题】:Convert ISO 8601 to NSDate将 ISO 8601 转换为 NSDate
【发布时间】:2013-07-07 16:29:49
【问题描述】:

我有一个来自服务器的时间戳,如下所示:

2013-04-18T08:49:58.157+0000

我已经尝试删除冒号,我已经尝试了所有这些:

Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?

Why NSDateFormatter can not parse date from ISO 8601 format

这是我现在的位置:

+ (NSDate *)dateUsingStringFromAPI:(NSString *)dateString {


    NSDateFormatter *dateFormatter;
    dateFormatter = [[NSDateFormatter alloc] init];

    //@"yyyy-MM-dd'T'HH:mm:ss'Z'" - doesn't work
    //@"yyyy-MM-dd'T'HH:mm:ssZZZ" - doesn't work
    //@"yyyy-MM-dd'T'HH:mm:sss" - doesn't work 

    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

    // NSDateFormatter does not like ISO 8601 so strip the milliseconds and timezone
    dateString = [dateString substringWithRange:NSMakeRange(0, [dateString length]-5)];

    return [dateFormatter dateFromString:dateString];
}

我最大的问题之一是,我上面的日期格式真的是 ISO 8601 吗?我从人们那里看到的所有示例,每个示例的格式都略有不同。有些人有...157-0000,有些人最后什么都没有。

【问题讨论】:

  • 你需要yyyy-MM-dd'T'HH:mm:ss.sssZ
  • 没有骰子,我什至尝试删除该行以删除最后 5 个字符。
  • 抱歉,我打错了 - 应该是 yyyy-MM-dd'T'HH:mm:ss.SSSZ。不要修剪字符串,不要设置时区。

标签: objective-c formatting nsdate nsdateformatter iso


【解决方案1】:

这对我有用:

NSString *dateString = @"2013-04-18T08:49:58.157+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
// Always use this locale when parsing fixed format date strings
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:posix];
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"date = %@", date);

【讨论】:

  • 做到了!非常感谢您的帮助
  • 是的,它可以在没有语言环境的情况下工作,但在某些情况下(各种用户设置)它不会。为确保您的代码始终有效,请将语言环境设置为特殊的 posix 语言环境。
  • 应该说如果你真的想要时间戳格式你想把最后的NSLog语句改成如下:NSLog(@"date = %f", [date timeIntervalSince1970]) ;
  • 另请注意,如果您经常调用 NSDateFormatters(例如,搅动图表数据点),分配 NSDateFormatters 会影响性能,因此您只想分配一次。
  • @Jean-DenisMuys 这个问题真的不是关于所有可能的 ISO 8601 日期格式,而只是其中一种。您需要尝试每种单独的格式以查看哪个返回非零NSDate。如果您需要帮助,您应该提出自己的问题。
【解决方案2】:

Apple 推出了新的 API! NSISO8601DateFormatter

NSString *dateSTR = @"2005-06-27T21:00:00Z";
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
NSDate *date = [formatter dateFromString:dateSTR];
NSLog(@"%@", date);

【讨论】:

    【解决方案3】:

    我还有原生 API,它更简洁...这是我在 DateTimeManager 类中得到的实现:

    + (NSDate *)getDateFromISO8601:(NSString *)strDate{
    
        NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
        NSDate *date = [formatter dateFromString: strDate];
        return date;
    }
    

    只需复制并粘贴该方法即可。尽情享受吧!

    【讨论】:

      【解决方案4】:

      最适合我的完美解决方案是:

      let isoFormatter = ISO8601DateFormatter();
      isoFormatter.formatOptions = [ISO8601DateFormatter.Options.withColonSeparatorInTime,
                                            ISO8601DateFormatter.Options.withFractionalSeconds,
                                            ISO8601DateFormatter.Options.withFullDate,
                                            ISO8601DateFormatter.Options.withFullTime,
                                            ISO8601DateFormatter.Options.withTimeZone]
      let date = isoFormatter.date(from: dateStr);
      

      更多细节可以参考苹果官方文档:https://developer.apple.com/documentation/foundation/nsiso8601dateformatter

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-01
        • 2012-02-13
        • 2018-05-26
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多