0

For example I have this code:

[self.df setDateFormat:@"z"]

which outputs with GMT I want an output of it as GMT+1

or GMT-4 to GMT-3 or GMT+9 to GMT+10

Is that possible with NSTimeZone?

user3517855
  • 211
  • 1
  • 3
  • 12
  • Look for `NSDateFormatter`, the doc and many examples on SO. What have you tried? – Larme Apr 18 '14 at 14:39
  • 5
    Confusing question. Do you need to append string or offset the date object? – Desdenova Apr 18 '14 at 14:39
  • 1
    Yes, it is possible with NSTimeZone. On the other hand it may not even be required bothering about at all, if you just use NSDate accordingly along with NSLocale. But without any code you can hardly expect a good answer to the "how" part of your question – Hermann Klecker Apr 18 '14 at 14:43
  • Did you try my answer ? – limon Apr 18 '14 at 20:56

1 Answers1

0

I've made it work by doing following.

NSTimeZone *timeZone = [NSTimeZone localTimeZone] ; 

to get my local time zone.

In NSTimeZone class, there is method called secondsFromGMT. From doc:

which returns the current difference in seconds between the receiver and Greenwich Mean Time.

I'm in GMT+02, in my case it returns 7200. As 7200 in seconds, there is two hours difference between my time zone and Greenwich.

Then, you can create a new time zone using timeZoneForSecondsFromGMT methods. As you want to add one hour, you need to add 3600 seconds to GMT. So, here is the code.

NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
NSInteger secondsFromGMT = [localTimeZone secondsFromGMT];
NSTimeZone *timeZoneYouWanted = [NSTimeZone timeZoneForSecondsFromGMT:secondsFromGMT + 3600];

I've tested timeZoneYouWanted is in GMT+03.

limon
  • 3,222
  • 5
  • 35
  • 52