views:

248

answers:

2

Hi, everyone.

I am working on a project which involved in converting current time on iPhone to target destination's time. For example, the application needs to convert current time (08:00) to Germany's local time.

I have information about timezone (like UTC +1) and tried to search about how to use NSTimeZone to convert the NSDate value, but it looks like I have to ask your help here.

Could you give me any suggestion? or point me out for some solution?

A: 

Check out NSDateFormatter and its setTimeZone: method.

gerry3
+1  A: 

The simplest thing is probably to use -[NSTimeZone secondsFromGMTForDate:] for each time zone, and then figure out the delta between the two:

NSInteger  offset1 = [timezone1 secondsFromGMTForDate: date];
NSInteger  offset2 = [timezone2 secondsFromGMTForDate: date];

return [date addTimeInterval: (offset1 - offset2);
Ben Gottlieb
Thank you, Ben. But how can I initial timezone object? for example, do I need to convert from local date in time zone (+7) to destination date in timezone (+1).
Teerasej
There are a number of initializers for NSTimeZone; probably use either +timeZoneForSecondsFromGMT: or + timeZoneWithName:. You mentioned that you already had information about the timezone.
Ben Gottlieb