-1

I am trying to use NSDateFormatter to convert the following string to an NSDate: 2013-08-19 7:00 AM.

However, the following NSDate is created: 2013-08-19 04:00:00 +0000. The hour is wrong.

My code is below. I don't know what I am doing incorrectly.

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm a"];
NSString *string = @"2013-08-19 7:00 AM";
NSDate *dateFromString = [dateFormatter dateFromString:string];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
  • Maybe the problem is that you're not specifying a time-zone. What time-zone are you on? – LuisCien Aug 19 '13 at 18:52
  • @LuisCien I didn't realize that mattered. I want to create two date objects (such as for 7 AM and for 11 AM) and count the seconds between the two times. – Jack Humphries Aug 19 '13 at 18:53
  • I had this "problem" before too. Not really a problem, but you have no time zone set. NSDate does not include the time zone. – Jamie Aug 19 '13 at 18:54
  • @Jamie Should I set it to the local timezone? – Jack Humphries Aug 19 '13 at 18:55
  • 1
    Yes. If that's where you're getting the time from. It would be wherever the time is coming from. – Jamie Aug 19 '13 at 18:55
  • 1
    @JackHumphries There is no need to set the formatter's timezone if you want the local timezone - that is the default behavior. – rmaddy Aug 19 '13 at 19:26
  • @JackHumphries When you log an `NSDate` object it is always logged in UTC (+0000). That is normal. – rmaddy Aug 19 '13 at 19:28
  • possible duplicate of [Getting date from \[NSDate date\] off by a few hours](http://stackoverflow.com/q/8466744) – jscs Aug 19 '13 at 19:36

2 Answers2

4

You're attempting to use a 24-hour format for the hours and an AM/PM indicator, and that just won't work. Change your line:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm a"];

to this:

[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm a"];

and you'll find that your code now works.

Extra Savoir-Faire
  • 6,026
  • 4
  • 29
  • 47
-2

The reason why you see "+0000" means that the OS is converting your time string to UTC. To solve this you just have to set your local time-zone.

dateFormatter.timeZone = YOUR_LOCAL_TIMEZONE;

Also, to print your date in your local settings you can use NSDateFormatter's:

localizedStringFromDate:dateStyle:timeStyle:

Hope this helps!

LuisCien
  • 6,362
  • 4
  • 34
  • 42
  • Is the correct: `[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];` – Jack Humphries Aug 19 '13 at 18:57
  • It's still returning 4 AM. – Jack Humphries Aug 19 '13 at 18:57
  • 1
    @LuisCien No, the OS is not converting any string. The issue is simple - when you log an `NSDate` object, the `description` method is called on the `NSDate`. The implementation of this method is to return the date as a string in UTC time. None of this has anything to do with the actual parsing of the string into an `NSDate` using the `NSDateFormatter` (which assumes locale time by default). – rmaddy Aug 19 '13 at 19:30
  • The reason he sees +0000 is that NSLog of an NSDate always returns UTC. – Hot Licks Aug 19 '13 at 20:00
  • Thanks for shedding some light on me :) – LuisCien Aug 22 '13 at 00:30