NSDateFormatter时区(Etc / GMT)未被解析为dateFromString

时间:2011-07-11 13:37:02

标签: iphone datetime timezone

我试图通过以下代码将此2011-07-11 13:28:59 Etc/GMT转换为NSDate来获取nil

NSString *dateString=@"2011-07-11 13:28:59 Etc/GMT";
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];
NSDate *purchaseDate = [dateformatter dateFromString:dateString];

此日期来自苹果服务器本身,用于应用内购买验证。我用时区去了很多线程。但它们都没有显示如何转换此Etc/GMT

我尝过z zz zzz zzzz Z ZZ ZZZ ZZZZ

这里可能存在什么问题。

2 个答案:

答案 0 :(得分:6)

// Swift版本

    let DateString = "2016-01-21 00:29:09 Etc/GMT"
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
    var dateObject = dateFormatter.date(from: DateString)

答案 1 :(得分:3)

NSString *dateString=@"2011-07-11 13:28:59 Etc/GMT";
dateString = [dateString stringByReplacingOccurrencesOfString:@"Etc/GMT" withString:@"GMT"];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

// Or stuff....>>

NSDate *date = [dateformatter dateFromString: dateString];
NSLog(@"You may Need This  =========%@",date);
dateformatter = [[[NSDateFormatter alloc] init] autorelease];
[dateformatter setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];

NSString *convertedString = [dateformatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);

///可以帮助=====>> http://unicode.org/reports/tr35/