0

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?

Community
  • 1
  • 1
juan Isaza
  • 3,646
  • 3
  • 31
  • 37
  • 2
    This is a very common question/issue. Time zone is what you need to search for. – trojanfoe May 23 '14 at 05:02
  • Why do you set the data formatter's format and locale twice? – rmaddy May 23 '14 at 05:03
  • You left out the important part - how do you log the result? – rmaddy May 23 '14 at 05:04
  • It will doubtless be `NSLog()`; another source of confusion... – trojanfoe May 23 '14 at 05:04
  • @trojanfoe I don't think it is a time zone issue, because it is working perfectly well for the format "yyyy-MM-dd HH:mm:ss ZZZ". Or it is a time zone format dependent? – juan Isaza May 23 '14 at 05:16
  • @rmaddy I just deleted the US format part, but as expected it is still not working. I log with a breakpoint. – juan Isaza May 23 '14 at 05:16
  • 1
    The important thing to realise is that `[NSDate description]` (what is used when feeding a date to `NSLog()` and what is seen within the debugger) will show the date in UTC, not local time. You need to use a data formatter to display the date in the time zone you desire. – trojanfoe May 23 '14 at 05:18
  • @Martin R, after performing some test. I got to the conclusion that this question is NOT a duplicate. If the time zone was the problem in my country would be 5 hours off not 2 or 3. What is happening is that the string "yyyy-MM-dd HH:mm:ss a ZZZ" in my code should be "yyyy-MM-dd hh:mm:ssa ZZZ". So the hour is not taken properly, can you please remove duplicate status in order to post a proper answer? thank you. – juan Isaza May 24 '14 at 17:04
  • Your date format string is self-contradictory, since you specify both HH and a. – Hot Licks May 24 '14 at 17:22
  • 1
    So HH is for 00-23 hours, hh is for 00-11. If you use AM/PM symbols, you should use hh. Maybe that was causing the issue... – Tiago Lira May 24 '14 at 17:43

0 Answers0