如何获得全时区名称ios

时间:2015-07-10 10:39:09

标签: ios objective-c xcode timezone nstimezone

我们如何在iOS中获取全时区名称 喜欢印度标准时间而不是IST。

NSTimezone *timeZone=[NSTimeZone localTimeZone];

我正在获得GMT,但我希望它像完整的表单字符串

3 个答案:

答案 0 :(得分:12)

您可以从name属性中获取名称:

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

结果:

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

您可以使用NSTimeZoneNameStyle

获取所需样式- localizedName:locale:的名称
NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(@"Name : %@", timeZoneName);

结果:

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";
}

答案 1 :(得分:8)

试试这个;

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

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

答案 2 :(得分: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);

}

输出就像......

东部标准时间---美国/纽约(EDT)偏移-14400(日光)大西洋标准时间---美国/哈利法克斯(ADT)偏移-10800(日光)