Anas Abu Farraj
Anas Abu Farraj

Reputation: 1598

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:];

Upvotes: 2

Views: 647

Answers (1)

W.K.S
W.K.S

Reputation: 10095

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]

Upvotes: 2

Related Questions