2

I'm in PDT (pacific daylight time) which is -0700 hrs behind UTC. I'm using NSDateFormatter to parse a time string, 4:00 PM, and then create a date:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSDate *aDate = [[NSDate alloc] init];
[df setDateFormat:@"h:mm a"];
aDate = [df dateFromString:@"4:00 PM"];

aDate returns as: 1970-01-02 00:00:00 +0000. However, I'd expect it to return: 1970-01-01 23:00:00 +0000 since 4PM should be 23:00 UTC.

Checking the date formatter time zone returns the correct (PDT) time zone, ie: America/xxx (PDT) offset -25200 (Daylight)

Does anybody know what is going on here? Thanks in advance for the help...

Joey J
  • 1,355
  • 10
  • 26

1 Answers1

2

Without specifying the date, you'll be in PST, not PDT. Try it again with:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEE, d LLL yyyy hh:mm a"];
NSDate *aDate = [df dateFromString:@"Sat, 21 Apr 2012 04:00 PM"];
NSLog(@"date=%@", aDate);
danh
  • 62,181
  • 10
  • 95
  • 136
  • The code above returns: 2012-04-21 19:00:00 +0000. 19:00:00 is 7pm which I can't explain how that is calculated. – Joey J Apr 22 '12 at 03:47
  • 7 - 7 = 0. Run it through this, where sourceDate = aDate; http://stackoverflow.com/questions/4547379/nsdate-is-not-returning-my-local-time-zone-default-time-zone-of-device. – danh Apr 22 '12 at 03:58
  • danh I don't understand your comment. 7-7=0? – Joey J Apr 22 '12 at 04:09
  • @JakeV - geez, I thought I understood it when I answered (1900 is 7pm, minus 7 is 12:00), but maybe that was a late night (PDT) brain-fart. Now I'm confused, too. Let me think about this and get back to you. – danh Apr 22 '12 at 16:43
  • 2
    Changed the "HH" to "hh", since you're using a 12-hour clock. – Ken Thomases Apr 22 '12 at 20:03
  • @KenThomases is spot on. Nice catch. +1. Will edit. Ran it (like I should have to begin with) and it works as expected. – danh Apr 22 '12 at 20:51