如何将时区显示为文本?

时间:2011-06-30 15:50:17

标签: iphone objective-c ios

我希望能够在我的应用启动时将系统时区显示为单独的纯文本:

- (void)viewDidLoad {
    [super viewDidLoad];

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    NSString* date = [dateFormatter stringFromDate:[NSDate date]];
    lblDate.text = date;

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSString* zone = [gmt:[NSTimeZone defaultTimeZone]];
txtTZ.text = zone;`

我的应用可以正确显示日期,但会在时区崩溃。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

尝试替换它:

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSString* zone = [gmt:[NSTimeZone defaultTimeZone]];
txtTZ.text = zone;

有了这个:

NSString *zone = [[NSTimeZone systemTimeZone] abbreviation];
txtTZ.text = zone;