-2

Possible Duplicate:
Getting date from [NSDate date] off by a few hours

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"M-d-yyyy H:mm"];

NSDate *start= [dateFormatter dateFromString:@"10-24-2012 12:15"];
NSDate *end  = [dateFormatter dateFromString:@"10-24-2012 15:30"];

When I print out

   NSLog(@"------main_event start %@", start);
   NSLog(@"-----main_event end %@", end);

The result is

         ---main_event start 2012-10-24 19:15:00 +0000
         ---main_event end 2012-10-24 22:30:00 +0000

Now, it looks like the time added 7 hours automatically, 12:15 becomes 19:15, and 15:30 becomes 22:30.

Why?

Community
  • 1
  • 1
jason white
  • 687
  • 4
  • 11
  • 28

2 Answers2

1

because the timezone, where your device is located, is UTC-7.

The output is in UTC (hence the +0000), as a single NSDate will always print out it's time in UTC.

If you use an NSDateFormatter to output the date, it will take your locale in account. See my answer here: NSDate date method returns wrong result

Community
  • 1
  • 1
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

These are correct results. When you use NSLog to output an NSDate object, it displays in GMT. The parsing was done in your local timezone. NSDate objects are alway in GMT. If you want to print the NSDate object in your local timezone then you need an NSDateFormatter to print the date.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Note, that this is not totally correct: It is UTC, as GMT can differ from UTC due to the daylight saving time. GMT == (UTC+0|UTC+1) http://en.wikipedia.org/wiki/Greenwich_Mean_Time – vikingosegundo Oct 24 '12 at 00:19
  • @vikingosegundo Actually the docs state the time is output offset from GMT. – rmaddy Oct 24 '12 at 00:23
  • If you change the clock of you computer, and the location of the simulator to London, you will see, that for some dates you get +0100, na din winter +0000. I tried it out years ago. also: «UTC is synonymous with GMT, but GMT is no longer precisely defined by the scientific community.» http://en.wikipedia.org/wiki/UTC – vikingosegundo Oct 24 '12 at 00:32
  • I understand there is a difference between **UTC** and **GMT**. I'm saying the docs for `NSDate` state that the timezone used in the `description` method is **GMT**. – rmaddy Oct 24 '12 at 00:34
  • the docs are wrong. but the terms are easy to mix up. – vikingosegundo Oct 24 '12 at 00:35
  • May I suggest you submit a documentation bug report to Apple. There is a link at the bottom of each page in the docs (at least in Xcode, not sure about online). – rmaddy Oct 24 '12 at 00:37
  • OK. That's an odd response from someone that is taking the time to offer their knowledge here to help others. Your choice of course. :) – rmaddy Oct 24 '12 at 00:41
  • the funny thing about the class reference of NSDate: they only chose example dates in winter, when there is no difference between GMT and UTC. – vikingosegundo Oct 24 '12 at 00:41
  • Let me say: I just wanted to be as pedantic, as you had been on my answer here: http://stackoverflow.com/questions/12975559/how-to-format-data-from-string-variable/12975760#12975760 – vikingosegundo Oct 24 '12 at 00:42