I found this weird issue, when converting from string to a NSDate. The final date is wrong by exactly 2 or 3 hours (possible an integer number of hours). My data has an excellent quality as it was picked from other ios devices programmatically. For example when I try to convert:
"2014-05-23 03:14:04 a.m. +0000"
I get:
2014-05-23 00:14:04 +0000
or, when converting:
"2014-05-23 02:49:30 a.m. +0000"
I get:
2014-05-23 00:49:30 +0000
The date format is in Spanish and therefore my code is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a ZZZ"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"es"]];
[dateFormatter setAMSymbol:@"a.m."]; // default AM symbol for spanish is a.m.
[dateFormatter setPMSymbol:@"p.m."]; // default PM symbol for spanish is p.m.
// Set the date format for the input string
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a ZZZ"];
newEvent.time = [dateFormatter dateFromString:[timeArray objectAtIndex:i]];
according to:
https://stackoverflow.com/a/13757666/2394901
but with a modification for a.m. and p.m. instead of AM and PM because that way the answer is nil.
UPDATE:
This issue is not due to time zone difference as suggested below. Instead the proper solution is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"es"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ssa ZZZ"];
newEvent.time = [dateFormatter dateFromString:[timeArray objectAtIndex:i]];
**the problem is the capital letters HH, should be hh. Anybody knows when should letters must by capitalized?