【发布时间】:2014-09-30 12:36:28
【问题描述】:
我想用时区 PET 解析来自 API 的日期字符串。所以我创建了 NSDateFormatter 并将字符串转换为日期,但不幸的是它不起作用。结果我得到了零。有什么解决方法吗?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"EEE MMM dd HH:mm:ss yyyy zz"];
NSDate *newStartDate = [formatter dateFromString:@"Mon Sep 29 14:40:00 2014 PET"];
NSLog(@"newStartDate - %@",newStartDate);
【问题讨论】:
-
NSTimeZone knownTimeZones 返回可识别的时区列表。赔率是“PET”不在列表中。
-
但是使用 [NSTimeZone timeZoneWithAbbreviation] 会得到一个列表,其中有 PET。如果我们使用 [NSTimeZone knownTimeZoneNames];那么“美国/利马”就是 PET
-
NSDateFormatter 往往对读取带有“EEE”的格式感到挑剔——有时它有效,有时无效。最好在解析之前从字符串中去除星期几的值(当然,适当地修改格式字符串)。另外,请注意"locale feature"。
标签: ios objective-c iphone nsdate nsdateformatter