I am trying to formate email dates but date formatter returns nil where last five characters -0800. Doesn't have any problem with date has +0000. I have list of mixed dates of emails ending with +0000 and -0800
How can I add that 8 hours difference to my date to make it +0000? Some emails have sent from LA date origin as 13 Jan 2020 05:22:03 -0800. But, I am accessing in UK with 8 hours difference.
13 Jan 2020 05:22:03 -0800 should look a like 13 Jan 2020 13:22:03 +0000
- (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;
}
13 Jan 2020 05:22:03 -0800 is the specific date returns nil. Any help is appriciated.
I tried below statements
[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"];
The date string does not contain a weekday, so the specifier EEE
is wrong anyway.
Set the locale to en_US_POSIX
and use the format dd MMM yyyy HH:mm:ss Z
By the way there is a shorter and more convenient syntax
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
formatter.dateFormat = @"dd MMM yyyy HH:mm:ss Z";
You can find the complete list of format specifiers on unicode.org