【问题标题】:NSString to NSDate = nil (with timezone)NSString 到 NSDate = nil(带时区)
【发布时间】:2020-05-25 18:47:27
【问题描述】:

尝试将“expires_date”字符串值转换为 NSDate 对象。

  • 日期字符串 = "2019-08-18 15:12:09 America/Los_Angeles"

有什么建议吗?

-(NSDate*)expires_date{
    NSMutableDictionary * dictionary = [_dictionary mutableCopy];
    NSString * date_string = [dictionary valueForKey:@"expires_date"];

    //date_string = "2019-08-18 15:12:09 America/Los_Angeles"

    NSDateFormatter *dateFormatter = [IAPSKReceipt formatter];
    NSDate *date = [dateFormatter dateFromString:date_string];
    return date;
}

+(NSDateFormatter*)formatter{
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    dateFormatter = @"yyyy-MM-dd HH:mm:ss";
    dateFormatter = [NSTimeZone timeZoneForSecondsFromGMT:0];
    return dateFormatter;
}

【问题讨论】:

    标签: in-app-purchase nsdateformatter in-app-purchase-receipt


    【解决方案1】:

    您可能想查看 ISO8601DateFormatter,它是为您尝试做的事情量身定制的:

    https://developer.apple.com/documentation/foundation/iso8601dateformatter

    【讨论】:

      【解决方案2】:

      除了@Shai Mishali 的回答:

      考虑到时区,您的日期似乎既不符合 RFC3339 也不符合 ISO8601。

      ISO8601 allows substituting the 'T' in the middle for a ' ' 所以你不会得到 nil 响应。但是,由于时区不符合规范,因此不会对其进行解析,并且预期的响应不会在所需的 LA 时区(PST、GMT-8)中。

      这是您在合法 ISO8601/RFC3339 中的日期:

      "2019-08-18T15:12:09-0800"

      【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 2014-05-11
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2011-02-06
      • 2014-05-19
      相关资源
      最近更新 更多