anasabufarraj anasabufarraj - 5 months ago 20
Objective-C Question

How to set the timezone on NSDateComponents

I'm trying to encapsulate a time using a

NSDateComponents
..but it give me 1979-05-10 08:30:00 +0000....what value should set for
setTimeZone:
to give me the Houres in my local time (GMT+2) which should be 1979-05-10 10:30:00 +0000

NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setYear:1979];
[comp setMonth:5];
[comp setDay:10];
[comp setHour:10];
[comp setMinute:30];
[comp setTimeZone:];

Answer

You can use +(NSTimeZone) timeZoneForSecondsFromGMT:. To convert hours to seconds, multiply by 3600.

Examples:

For GMT+2

[NSTimeZone timeZoneForSecondsFromGMT:3600*2]

For GMT-2

[NSTimeZone timeZoneForSecondsFromGMT:-3600*2]

To get the timezone local to your device:

[NSTimeZone localTimeZone]