NSDateFormatter-带有时区的NSString返回nil

问题描述 投票:0回答:1

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

issue:NSDate对象=无。

  • 日期字符串= 2019-08-18 15:12:09 America / Los_Angeles
  • 日期格式= @“ yyyy-MM-dd HH:mm:ss”

有什么建议吗?

#define K_RECEIPT_TIMEZONE_FORMAT @"yyyy-MM-dd HH:mm:ss"

-(NSDate*)expires_date{
    NSMutableDictionary * dictionary = [_dictionary mutableCopy];
    NSString * date_string = [[dictionary valueForKey:@"expires_date"] copy];
    NSDateFormatter *dateFormatter = [IAPSKReceipt formatter];
    NSDate *date = [dateFormatter dateFromString:date_string];
    return date;
}

+(NSDateFormatter*)formatter{

    NSDateFormatter * RFC3339DateFormatter = [[NSDateFormatter alloc] init];
    if (@available(iOS 10.0, *)) {
        RFC3339DateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
    } else {
        RFC3339DateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    }
    RFC3339DateFormatter.dateFormat = K_RECEIPT_TIMEZONE_FORMAT;
    RFC3339DateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    return RFC3339DateFormatter;
}
in-app-purchase nsdateformatter in-app-purchase-receipt
1个回答
1
投票

您可能要研究针对您要执行的操作而量身定制的ISO8601DateFormatter:

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

© www.soinside.com 2019 - 2024. All rights reserved.