2

When i call [NSDate date] it returns 2 hours less than it should. I've checked my computer clock settings and they are all ok. I've checked settings in iPhone and time zone and clock are all OK. Even simulator shows correct time on top toolbar. But when I try to log current date it shows 2 hours less than it should. I ran out of ideas where to look.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
paxx
  • 1,079
  • 2
  • 12
  • 26
  • What time zone does the log output report? What time zone are you in? It's probably just that: 10:00 in GMT is not 2 hours earlier than 12:00 in +02:00, it's the same time, just in a different format. – Ole Begemann Apr 12 '11 at 07:57
  • Check the calendar? Is it gregorian calendar? Try to check your timezone using `NSLog(@"%@", [[NSTimeZone systemTimeZone] description])` – lxcid Apr 12 '11 at 07:59
  • it's gregorian. When i log systemTimeZone i get an offset of 7200 which is exactly 2h that i'm missing. but I still cant figure it out whats the best practice to use local time. i guess it's something with a formatter but i still didnt get what? – paxx Apr 12 '11 at 15:03

3 Answers3

1

All dates returned by [NSDate date] are in the GMT time zone.

When you use any NSDateFormatter, just set the time zone and it will print out the correct time.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • :D this is the kind of thing that always gets me. When all else fails, RTFM! –  Apr 12 '11 at 07:57
  • 3
    Actually, the object is supposed to contain just time, which doesn't depend on location, the rest depends on how you extract and display it and this is where GMT seems to chime in ;-) – Michael Krelin - hacker Apr 12 '11 at 07:59
0

Look carefully at the output of the NSLog() statement. You will see that the output always contains the timezone using standard UTC. Therefore, the date is actually correct, taking into account the timezone.

Massimo Cafaro
  • 25,429
  • 15
  • 79
  • 93
  • Tnx, but i still dont get te best practice in this situation. how can i get current local time? via formatter? – paxx Apr 12 '11 at 15:04
0

I found my solution here: How to convert time to the time zone of the iPhone device?

So if you ever want to change [NSDate date] to point to local time just use the code provided in the link above. And dont forget to change timeZoneWithAbbreviation on sourceTimeZone from EST to GMT (because NSDate is always in GMT)

Once again thanks everyone for helping out..

Community
  • 1
  • 1
paxx
  • 1,079
  • 2
  • 12
  • 26