2015-07-10 255 views
6

我們如何獲得iOS中的全時區名稱 (如印度標準時間)而不是IST。如何獲得全時區名稱ios

NSTimezone *timeZone=[NSTimeZone localTimeZone]; 

我得到GMT,但我希望它像全格式字符串

回答

12

您可以從name屬性獲取名稱:

NSLog(@"Name : %@", timeZone.name); 

結果:

2015-07-10 13:31:46.292 APP[2904:50429] Name : Asia/Calcutta 

你可以得到的名稱的Wi日使用- localizedName:locale:

NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"]; 
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]]; 
NSLog(@"Name : %@", timeZoneName); 

結果所需的樣式NSTimeZoneNameStyle

2015-07-10 13:36:31.771 APP[2940:51197] Name : India Standard Time 

如果你打電話[NSTimeZone abbreviationDictionary]你會得到一個包含所有可用的縮寫字典。

NSLog(@"%@",[NSTimeZone abbreviationDictionary]); 

結果:

2015-07-10 11:44:05.954 APP[1472:26120] { 
    ADT = "America/Halifax"; 
    AKDT = "America/Juneau"; 
    AKST = "America/Juneau"; 
    ART = "America/Argentina/Buenos_Aires"; 
    AST = "America/Halifax"; 
    BDT = "Asia/Dhaka"; 
    BRST = "America/Sao_Paulo"; 
    BRT = "America/Sao_Paulo"; 
    BST = "Europe/London"; 
    CAT = "Africa/Harare"; 
    CDT = "America/Chicago"; 
    CEST = "Europe/Paris"; 
    CET = "Europe/Paris"; 
    CLST = "America/Santiago"; 
    CLT = "America/Santiago"; 
    COT = "America/Bogota"; 
    CST = "America/Chicago"; 
    EAT = "Africa/Addis_Ababa"; 
    EDT = "America/New_York"; 
    EEST = "Europe/Istanbul"; 
    EET = "Europe/Istanbul"; 
    EST = "America/New_York"; 
    GMT = GMT; 
    GST = "Asia/Dubai"; 
    HKT = "Asia/Hong_Kong"; 
    HST = "Pacific/Honolulu"; 
    ICT = "Asia/Bangkok"; 
    IRST = "Asia/Tehran"; 
    IST = "Asia/Calcutta"; 
    JST = "Asia/Tokyo"; 
    KST = "Asia/Seoul"; 
    MDT = "America/Denver"; 
    MSD = "Europe/Moscow"; 
    MSK = "Europe/Moscow"; 
    MST = "America/Denver"; 
    NZDT = "Pacific/Auckland"; 
    NZST = "Pacific/Auckland"; 
    PDT = "America/Los_Angeles"; 
    PET = "America/Lima"; 
    PHT = "Asia/Manila"; 
    PKT = "Asia/Karachi"; 
    PST = "America/Los_Angeles"; 
    SGT = "Asia/Singapore"; 
    UTC = UTC; 
    WAT = "Africa/Lagos"; 
    WEST = "Europe/Lisbon"; 
    WET = "Europe/Lisbon"; 
    WIT = "Asia/Jakarta"; 
} 
+0

但我需要像印度標準時間而不是IST,我的意思是全名 謝謝 –

+0

謝謝,但我的關注是一樣的我需要全名,就像我們在窗口 –

+1

那樣,你需要獲得所需的樣式名稱[NSTimeZoneNameStyle](https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/參考/基礎/類/ NSTimeZone_Class/index.html#// apple_ref/c/tdef/NSTimeZoneNameStyle)using [' - localizedName:locale:'](https://developer.apple.com/library/prerelease/ios/documentation /Cocoa/Reference/Foundation/Classes/NSTimeZone_Class/index.html#//apple_ref/occ/instm/NSTimeZone/localizedName:locale :) – Aladin

7

試試這個;

NSTimeZone* timeZone = [NSTimeZone localTimeZone]; 
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]]; 

NSLog(@"timeZoneName : %@", timeZoneName); 
+0

謝謝分配兄弟.....你救了我的日子 –

+0

@PAnKajKhatri請接受如果它適合你的答案。 – Shoaib

+0

是它爲我工作...謝謝 –

1

我認爲你正在尋找這個

NSDictionary *tempDict = [NSTimeZone abbreviationDictionary]; 
NSTimeZone* timeZone; 
NSString* timeZoneName; 

for(NSString *str in tempDict){ 
    timeZone = [NSTimeZone timeZoneWithAbbreviation:str]; 
    timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]]; 
    NSLog(@"%@ --- %@", timeZoneName,timeZone); 

} 

輸出會像...

東部標準時間---美國/紐約(美國東部時間)的偏移量-14400(日光)大西洋標準時間---美國/哈利法克斯(ADT)偏移-10800(日光)