【问题标题】:Changing of time zone时区变更
【发布时间】:2025-12-03 09:35:01
【问题描述】:

我正在做 Objective-c (iOS) 开发。我可以知道如何将 GMT +0 的默认时区更改为 GMT +8。 GMT +8 适用于新加坡。

这是我的代码:

- (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]; 
}

谢谢。

【问题讨论】:

    标签: iphone objective-c ios timezone gmt


    【解决方案1】:

    你可以使用,

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

    或,

    + (id)timeZoneForSecondsFromGMT:(NSInteger)seconds
    

    这将是你的情况,

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

    【讨论】: