Winona
Winona

Reputation: 2970

Changing of time zone

I'm doing objective-c (iOS) development. May I know how do I go about changing the default time zone of GMT +0 to GMT +8. GMT +8 is for Singapore.

Here is my code:

- (IBAction)dateChanged {
    NSDate *choice = [datePick date];
    NSString *words = [NSString alloc initWithFormat:@"%@", choice];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You have selected:" message:words delegate:nil cancelButoonTitle:@"OK" otherBUttonTitles:nil, nil];
    [alert show];
    [alert release];

    [words release]; 
}

Thanks.

Upvotes: 0

Views: 380

Answers (1)

karim
karim

Reputation: 15599

You can use,

NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

OR,

+ (id)timeZoneForSecondsFromGMT:(NSInteger)seconds

Which will be in your case,

[calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8*60*60]];

Upvotes: 1

Related Questions