Objective c 对于日期2020年1月13日05:22:03-0800,NSDateFormatter返回零
我试图格式化电子邮件日期,但日期格式化程序返回nil,其中最后五个字符为-0800。日期为+0000时没有任何问题。我有以+0000和-0800结尾的电子邮件的混合日期列表 我怎样才能在我的约会上加上8个小时的差异,使之达到+0000?一些电子邮件已从洛杉矶date origin发送至2020年1月13日05:22:03-0800。但是,我在英国的访问时间相差8小时 2020年1月13日05:22:03-0800应该看起来像2020年1月13日13:22:03+0000Objective c 对于日期2020年1月13日05:22:03-0800,NSDateFormatter返回零,objective-c,nsdate,nsdateformatter,nslocale,Objective C,Nsdate,Nsdateformatter,Nslocale,我试图格式化电子邮件日期,但日期格式化程序返回nil,其中最后五个字符为-0800。日期为+0000时没有任何问题。我有以+0000和-0800结尾的电子邮件的混合日期列表 我怎样才能在我的约会上加上8个小时的差异,使之达到+0000?一些电子邮件已从洛杉矶date origin发送至2020年1月13日05:22:03-0800。但是,我在英国的访问时间相差8小时 2020年1月13日05:22:03-0800应该看起来像2020年1月13日13:22:03+0000 - (NSDate *)
- (NSDate *) date
{
NSString *d = [headers objectForKey:@"date"];
if (d == NULL)
return NULL;
NSRange rng = [d rangeOfString:@" ("];
if (rng.location != NSNotFound)
d = [d substringToIndex:rng.location];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]];
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSDate *result = [formatter dateFromString:d];
[formatter release];
return result;
}
2020年1月13日05:22:03-0800是返回零的具体日期。任何帮助都是值得的
我尝试了以下陈述
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]];
[formatter setLocale:[[NSLocale currentLocale] autorelease]];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zz"];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzzz"];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZZZ"];
日期字符串不包含工作日,因此说明符
EEE
无论如何都是错误的
将区域设置设置为en_US\u POSIX
并使用格式dd-MMM-yyy-HH:mm:ss-Z
顺便说一下,还有一种更简短、更方便的语法
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
formatter.dateFormat = @"dd MMM yyyy HH:mm:ss Z";
您可以在上找到格式说明符的完整列表。我意识到我的错误。谢谢