2016-01-20 66 views
0

我已經搜索了很多關於谷歌這個問題,但沒有找到合適的解決方案。所有結果以日期和時間格式提供時區。IOS - 以GMT格式獲取設備的當前時區

我想知道如何以GMT格式獲取設備的時區。 一樣,我想只有這

+5:30

的時區。我怎樣才能做到這一點?

我已經試過這

NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone]; 

但它會給結果在加爾各答/新德里。 我也嘗試了其他答案,但沒有找到解決方案。

回答

1

我的建議是查看蘋果文檔NSTimeZone

如果你這樣做,你意識到有一種方法-secondsFromGMT返回格林威治時間秒。
也許你也可以創建自己的date formatter字符串作爲-dateFormat-dateFormat屬性,並直接獲取時區的字符串表示形式。類似@"ZZZZZ"
有關如何正確編碼日期格式的信息,請點擊here

+0

創建的日期格式,只有返回offse來自GMT的t聽起來像是正確的解決方案。 (投票)。這樣你就不必擔心模糊的格式問題。日期格式化程序會爲你做。 –

0
-(void)getCurrentTimeZoneMins 
{ 
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 
    float timeZoneOffset = [destinationTimeZone secondsFromGMTForDate:[NSDate date]]/60;; 
    NSLog(@"%f mins",timeZoneOffset); 
} 

-(void)printTimeZone 
{ 
    NSDateFormatter *localTimeZoneFormatter = [NSDateFormatter new]; 
    localTimeZoneFormatter.timeZone = [NSTimeZone localTimeZone]; 
    localTimeZoneFormatter.dateFormat = @"Z"; 
    NSString *localTimeZoneOffset = [localTimeZoneFormatter stringFromDate:[NSDate date]]; 
    NSLog(@"%@",localTimeZoneOffset); 
} 
1

試試這個:

NSDate *myDate = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateStyle:NSDateFormatterLongStyle]; 
    [dateFormatter setTimeStyle:NSDateFormatterLongStyle]; 
    [dateFormatter setDateFormat:@"ZZZ"]; 

    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Kolkata"]; 
    [dateFormatter setTimeZone:timeZone]; 

    NSString *dateString = [dateFormatter stringFromDate: myDate]; 
    NSLog(@"%@", dateString); 

OUTPUT:

欲瞭解更多信息,請閱讀@Andrea答案

相關問題